jquery 时间戳转换本地 年月日 时分秒毫秒格式
你可以使用JavaScript的Date对象来进行时间戳的转换。首先,将时间戳作为参数传递给Date对象,然后使用Date对象的方法来获取本地的年、月、日、时、分、秒和毫秒。
以下是一个示例代码:
var timestamp = 1597679093000; // 时间戳
var date = new Date(timestamp);
var year = date.getFullYear(); // 获取年份
var month = date.getMonth() + 1; // 获取月份(注意月份是从0开始的,所以要加1)
var day = date.getDate(); // 获取日期
var hour = date.getHours(); // 获取小时
var minute = date.getMinutes(); // 获取分钟
var second = date.getSeconds(); // 获取秒钟
var millisecond = date.getMilliseconds(); // 获取毫秒
var formattedDate = year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second + '.' + millisecond;
console.log(formattedDate); // 输出:2020-8-18 10:24:53.0
注意,上述代码中获取到的年、月、日、时、分、秒和毫秒都是本地时间。如果你想要获取的是UTC时间,可以使用对应的UTC方法,例如getUTCFullYear()、getUTCMonth()、getUTCDate()等
原文地址: https://www.cveoy.top/t/topic/hABC 著作权归作者所有。请勿转载和采集!