Home > Web Front-end > JS Tutorial > body text

How to convert between timestamp and date format using js

亚连
Release: 2018-06-21 14:43:27
Original
2723 people have browsed it

This article mainly introduces the conversion between js timestamp and date format in detail. It has certain reference value. Interested friends can refer to it.

The examples in this article share with you Android The specific code for displaying the Jiugongge picture is for your reference. The specific content is as follows

Convert timestamp to date format

var date = new Date(时间戳);获取一个时间对象。
下面是获取时间日期的方法
下面是获取时间日期的方法。
date.getFullYear();//获取完整的年份(4位,1970)
date.getMonth();//获取月份(0-11,0代表1月,用的时候记得加上1)
date.getDate();//获取日(1-31)
date.getTime();//获取时间(从1970.1.1开始的毫秒数)
date.getHours();//获取小时数(0-23)
date.getMinutes();//获取分钟数(0-59)
date.getSeconds();//获取秒数(0-59)
Copy after login

For example, I need a format like 2015-8-24_8-24-30

function formatDate(datetime) {
  var year = datetime.getFullYear(),
  month = (datetime.getMonth() + 1 < 10) ? &#39;0&#39; + (datetime.getMonth() + 1):datetime.getMonth() + 1,
  day = datetime.getDate() < 10 ? &#39;0&#39; + datetime.getDate() : datetime.getDate(),
  hour = datetime.getHours() < 10 ? &#39;0&#39; + datetime.getHours() : datetime.getHours(),
  min = datetime.getMinutes() < 10 ? &#39;0&#39; + datetime.getMinutes() : datetime.getMinutes(),
  sec = datetime.getSeconds() < 10 ? &#39;0&#39; + datetime.getSeconds() : datetime.getSeconds();
  return year + &#39;-&#39; + month + &#39;-&#39; + day + &#39;_&#39; + hour + &#39;-&#39; + min + &#39;-&#39; + sec;
}
Copy after login

Here datatime is a Date object, formatted datetime = new Date(time); time is a timestamp.

Convert date format to timestamp

var strtime = &#39;2014-04-23 18:55:49:123&#39;;
var date = new Date(strtime); 
//传入一个时间格式,如果不传入就是获取现在的时间了,这样做不兼容火狐。
// 可以这样做
var arr = strtime.replace(/ |:/g, &#39;-&#39;).split(&#39;-&#39;);
date = new Date(Date.UTC(arr[1], arr[2], arr[3], arr[4], arr[5]));

//三种方式获取:
time1 = date.getTime();
time2 = date.valueOf();
time3 = Date.parse(date);

//三种获取的区别:
第一、第二种:会精确到毫秒
第三种:只能精确到秒,毫秒将用0来代替
比如上面代码输出的结果(一眼就能看出区别):
  1398250549123
  1398250549123
  1398250549000
Copy after login

The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.

Related articles:

How to use iframe elements in vue components

How to implement nav navigation bar using vue

How to scroll up the web page express

The above is the detailed content of How to convert between timestamp and date format using js. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!