600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > yyyy-MM-dd HH:mm:ss和时间戳之间的转换

yyyy-MM-dd HH:mm:ss和时间戳之间的转换

时间:2019-09-25 08:17:15

相关推荐

yyyy-MM-dd HH:mm:ss和时间戳之间的转换

var date = new Date();

console.log(date);//获取当前的中国标准时间

console.log(date.getFullYear());//获取当前日期对象的年份(四位数字年份)

console.log(date.getMonth());//获取当前日期对象的月份(0 ~ 11)

console.log(date.getDate());//获取当前日期对象的日数(1 ~ 31)

console.log(date.getHours());//获取当前日期对象的小时(0 ~ 23)

console.log(date.getMinutes());//获取当前日期对象的分钟(0 ~ 59)

console.log(date.getSeconds());//获取当前日期对象的秒钟(0 ~ 59)

console.log(date.getMilliseconds());//获取当前日期对象的毫秒(0 ~ 999)

yyyy-MM-dd HH:mm:ss转换成时间戳

var stringTime = '-10-12 22:37:33';//将获取到的时间转换成时间戳var timestamp = Date.parse(new Date(stringTime));console.log(timestamp)

当前时间转换成时间戳

// 获取当前时间戳(以s为单位)var timestamp = Date.parse(new Date());timestamp = timestamp / 1000;//当前时间戳为:1403149534console.log("当前时间戳为:" + timestamp);

时间戳转换成yyyy-MM-dd HH:mm:ss

function timestampToTime(timestamp) {var date = new Date(timestamp * 1000);//时间戳为10位需*1000,时间戳为13位的话不需乘1000var Y = date.getFullYear() + '-';var M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1):date.getMonth()+1) + '-';var D = (date.getDate()< 10 ? '0'+date.getDate():date.getDate())+ ' ';var h = (date.getHours() < 10 ? '0'+date.getHours():date.getHours())+ ':';var m = (date.getMinutes() < 10 ? '0'+date.getMinutes():date.getMinutes()) + ':';var s = date.getSeconds() < 10 ? '0'+date.getSeconds():date.getSeconds();return Y+M+D+h+m+s;}timestampToTime(1403058804);console.log(timestampToTime(1403058804));//-06-18 10:33:24

将时间戳转换成其他格式

var timestamp = 1403058804;var newDate = new Date();newDate.setTime(timestamp * 1000);console.log(newDate.toDateString());// Wed Jun 18 console.log(newDate.toGMTString());// Wed, 18 Jun 02:33:24 GMT console.log(newDate.toISOString());// -06-18T02:33:24.000Zconsole.log(newDate.toJSON());// -06-18T02:33:24.000Z console.log(newDate.toLocaleDateString());// 6月18日 console.log(newDate.toLocaleString());// 6月18日 上午10:33:24 console.log(newDate.toLocaleTimeString());// 上午10:33:24 console.log(newDate.toString());// Wed Jun 18 10:33:24 GMT+0800 (中国标准时间)console.log(newDate.toTimeString());// 10:33:24 GMT+0800 (中国标准时间) console.log(newDate.toUTCString());// Wed, 18 Jun 02:33:24 GMT

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。