1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > 把数字(时间戳)转成日期格式

把数字(时间戳)转成日期格式

时间:2022-07-20 04:10:16

相关推荐

把数字(时间戳)转成日期格式

把时间错转化为 年月日时分秒

function formatDateTime(inputTime) {var date = new Date(inputTime);var y = date.getFullYear();var m = date.getMonth() + 1;m = m < 10 ? ('0' + m) : m;var d = date.getDate();d = d < 10 ? ('0' + d) : d;var h = date.getHours();h = h < 10 ? ('0' + h) : h;var minute = date.getMinutes();var second = date.getSeconds();minute = minute < 10 ? ('0' + minute) : minute;second = second < 10 ? ('0' + second) : second;return y + '-' + m + '-' + d + ' ' + ' ' + h + ':' + minute + ':' + second;};例如:formatDateTime(1555293600)====》结果是:"-04-15 10:00:00"

// 获取某个时间格式的时间戳

var date = new Date(); //时间对象

var str = date.getTime(); //转换成时间戳

var time = new Date(“-07-10 10:21:12”);

var str = time.getTime();

// 将时间戳换成时间格式字符串

var timestamp3 = 1403058804; //注意:要是拿到的是字符串,可以通过parseInt等方法转成数字才可以

var newDate = new Date();

newDate.setTime(timestamp3 * 1000);

// Wed Jun 18

console.log(newDate.toDateString());

// Wed, 18 Jun 02:33:24 GMT

console.log(newDate.toGMTString());

// -06-18T02:33:24.000Z

console.log(newDate.toISOString());

// -06-18T02:33:24.000Z

console.log(newDate.toJSON());

// 6月18日

console.log(newDate.toLocaleDateString());

// 6月18日 上午10:33:24

console.log(newDate.toLocaleString());

// 上午10:33:24

console.log(newDate.toLocaleTimeString());

// Wed Jun 18 10:33:24 GMT+0800 (中国标准时间)

console.log(newDate.toString());

// 10:33:24 GMT+0800 (中国标准时间)

console.log(newDate.toTimeString());

// Wed, 18 Jun 02:33:24 GMT

console.log(newDate.toUTCString());

封装的函数,可直接调用:

Base.RSFormatDat = function(dt, it) {

var _tTime = new Date(parseInt(dt) * 1000);

if (it == 0) {

var tHours = _tTime.getHours();

if (tHours < 10 && tHours >= 0) {

tHours = “0” + tHours;

}

var tMinutes = _tTime.getMinutes();

if (tMinutes < 10 && tMinutes >= 0) {

tMinutes = “0” + tMinutes;

}

var tSeconds = _tTime.getSeconds();

if (tSeconds < 10 && tSeconds >= 0) {

tSeconds = “0” + tSeconds;

}

return _tTime.getFullYear() + “年” + (parseInt(_tTime.getMonth(), 10) + 1) + “月” + _tTime.getDate() + "日 " + tHours + “:” + tMinutes + “:” + tSeconds;

} else {

return _tTime.getFullYear() + “年” + (parseInt(_tTime.getMonth(), 10) + 1) + “月” + _tTime.getDate() + “日”;

}

}

Base.RSFormatDat (第一个参数是要转化的时间戳,第二个参数是转化成决定要转成年月日还是年月日时分秒,前者传入1,后者传入0)

例如 把 1510151706转化成 x年x月x日

Base.RSFormatDat (1510151706,0)

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