Javascript 如何将毫秒转换为可读日期?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8579861/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 06:34:21  来源:igfitidea点击:

How to convert milliseconds into a readable date?

javascriptjquery

提问by Christian Fazzini

The following:

下列:

new Date(1324339200000).toUTCString()

Outputs:

输出:

"Tue, 20 Dec 2011 00:00:00 GMT"

I need it to return Dec 20. Is there a better method I can use besides toUTCString()? I am looking for any way to parse through milliseconds, to return a human readable date.

我需要它返回Dec 20。除了我可以使用的更好的方法toUTCString()吗?我正在寻找任何方法来解析毫秒,以返回人类可读的日期。

回答by pimvdb

Using the library Datejs you can accomplish this quite elegantly, with its toStringformat specifiers: http://jsfiddle.net/TeRnM/1/.

使用库 Datejs,您可以通过其toString格式说明符非常优雅地完成此操作:http: //jsfiddle.net/TeRnM/1/

var date = new Date(1324339200000);

date.toString("MMM dd"); // "Dec 20"

回答by Ravindra

You can use datejs and convert in different formate. I have tested some formate and working fine.

您可以使用 datejs 并转换为不同的格式。我已经测试了一些甲酸盐并且工作正常。

var d = new Date(1469433907836);

d.toLocaleString()     // 7/25/2016, 1:35:07 PM
d.toLocaleDateString() // 7/25/2016
d.toDateString()       // Mon Jul 25 2016
d.toTimeString()       // 13:35:07 GMT+0530 (India Standard Time)
d.toLocaleTimeString() // 1:35:07 PM
d.toISOString();       // 2016-07-25T08:05:07.836Z
d.toJSON();            // 2016-07-25T08:05:07.836Z
d.toString();          // Mon Jul 25 2016 13:35:07 GMT+0530 (India Standard Time)
d.toUTCString();       // Mon, 25 Jul 2016 08:05:07 GMT

回答by lonesomeday

No, you'll need to do it manually.

不,您需要手动执行此操作。

function prettyDate(date) {
  var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
                'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];

  return months[date.getUTCMonth()] + ' ' + date.getUTCDate() + ', ' + date.getUTCFullYear();
}

console.log(prettyDate(new Date(1324339200000)));

回答by ssamuel68

This is a solution. Later you can split by ":" and take the values of the array

这是一个解决方案。稍后您可以通过“:”拆分并获取数组的值

 /**
 * Converts milliseconds to human readeable language separated by ":"
 * Example: 190980000 --> 2:05:3 --> 2days 5hours 3min
 */
function dhm(t){
    var cd = 24 * 60 * 60 * 1000,
        ch = 60 * 60 * 1000,
        d = Math.floor(t / cd),
        h = '0' + Math.floor( (t - d * cd) / ch),
        m = '0' + Math.round( (t - d * cd - h * ch) / 60000);
    return [d, h.substr(-2), m.substr(-2)].join(':');
}

//Example
var delay = 190980000;                   
var fullTime = dhm(delay);
console.log(fullTime);

回答by Koushik Das

You can do it with just a few lines of pure js codes.

只需几行纯 js 代码即可完成。

var date = new Date(1324339200000);
    var dateToStr = date.toUTCString().split(' ');
    var cleanDate = dateToStr[2] + ' ' + dateToStr[1] ;
console.log(cleanDate);

returns Dec 20. Hope it helps.

12 月 20 日返回。希望它有所帮助。

回答by crashwap

Building on lonesomeday's example(upvote that answer not this one), I ran into this output:

lonesomeday的示例为基础(赞成答案不是这个),我遇到了这个输出:

undefined NaN, NaN

undefined NaN, NaN

var datetime = '1324339200000'; //LOOK HERE

function prettyDate(date) {
  var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
                'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];

  return months[date.getUTCMonth()] + ' ' + date.getUTCDate() + ', ' + date.getUTCFullYear();
}

console.log(prettyDate(new Date(datetime))); //AND HERE

The cause was using a string as input. To fix it, prefix the string with a plus sign:

原因是使用字符串作为输入。要修复它,请在字符串前加上加号:

prettyDate(new Date(+datetime));

prettyDate(new Date(+datetime));

var datetime = '1324339200000';

function prettyDate(date) {
  var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
                'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];

  return months[date.getUTCMonth()] + ' ' + date.getUTCDate() + ', ' + date.getUTCFullYear();
}

console.log(prettyDate(new Date(+datetime))); //HERE

To add Hours/Minutes to the output:

将小时/分钟添加到输出:

var datetime = '1485010730253';

function prettyDate(date) {
  var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
                'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];

  return months[date.getUTCMonth()] +' '+ date.getUTCDate()+ ', '+ date.getUTCHours() +':'+ date.getUTCMinutes();
}

console.log(prettyDate(new Date(+datetime)));

回答by user3669653

I just tested this and it works fine

我刚刚测试了这个,它工作正常

var d = new Date(1441121836000);

The data object has a constructor which takes milliseconds as an argument.

数据对象有一个以毫秒为参数的构造函数。