Javascript 如何将结果从 Date.now() 转换为 yyyy/MM/dd hh:mm:ss ffff?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30158574/
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-23 04:44:50  来源:igfitidea点击:

How to convert result from Date.now() to yyyy/MM/dd hh:mm:ss ffff?

javascriptdate

提问by RollRoll

I'm looking for something like yyyy/MM/dd hh:mm:ss ffff

我正在寻找类似的东西 yyyy/MM/dd hh:mm:ss ffff

Date.now()returns the total of milliseconds (ex: 1431308705117).

Date.now()返回总毫秒数(例如:1431308705117)。

How can I do this?

我怎样才能做到这一点?

回答by Sumner Evans

You can use the Dateconstructor which takes in a number of milliseconds and converts it to a JavaScript date:

您可以使用Date构造函数,该构造函数需要几毫秒并将其转换为 JavaScript 日期:

var d = new Date(Date.now());
d.toString() // returns "Sun May 10 2015 19:50:08 GMT-0600 (MDT)"

In reality, however, doing Date(Date.now())does the same thing as Date(), so you really only have to do this:

然而,实际上,Date(Date.now())does 与做同样的事情Date(),所以你真的只需要这样做:

var d = new Date();
d.toString() // returns "Sun May 10 2015 19:50:08 GMT-0600 (MDT)"

回答by Luis

You can use native JavaScript Date methods to achieve that or you can use a library like Moment.js.

您可以使用原生 JavaScript Date 方法来实现这一点,也可以使用像Moment.js这样的库。

It is a simple as:

这是一个简单的:

moment().format('YYYY/MM/D hh:mm:ss SSS')

If you are going use a lot of date formatting/parsing in your application then I definitely recommend using it.

如果您要在应用程序中使用大量日期格式/解析,那么我绝对推荐使用它。

回答by CONvid19

You can use Date().toISOString(), i.e.:

您可以使用Date().toISOString(),即:

let d = new Date().toISOString();
alert(d);


Output:

输出:

2015-05-11T01:56:52.501Z


Demo:

演示:

https://jsfiddle.net/h0eo9k4c/

https://jsfiddle.net/h0eo9k4c/

回答by Steephen

function formatted_date()
{
   var result="";
   var d = new Date();
   result += d.getFullYear()+"/"+(d.getMonth()+1)+"/"+d.getDate() + 
             " "+ d.getHours()+":"+d.getMinutes()+":"+
             d.getSeconds()+" "+d.getMilliseconds();
   return result;
}

console.log(formatted_date())

Output: "2015/5/10 22:5:26 429"

输出:“2015/5/10 22:5:26 429”

回答by Prashant rawal

function millisecondsToHuman(ms) {
    const seconds = Math.floor((ms / 1000) % 60);
    const minutes = Math.floor((ms / 1000 / 60) % 60);
    const hours = Math.floor(ms / 1000 / 60 / 60);

    const humanized = [
       pad(hours.toString(), 2),
       pad(minutes.toString(), 2),
       pad(seconds.toString(), 2),
    ].join(':');

return humanized;
}

function pad(numberString, size) {
   let padded = numberString;
   while (padded.length < size) padded = `0${padded}`;
   return padded;
}

回答by JPRukus75

var date = new Date();

will get you an answer formatted like this: Sun May 10 2015 21:55:01 GMT-0400 (Eastern Daylight Time)

会给你一个格式如下的答案:Sun May 10 2015 21:55:01 GMT-0400 (Eastern Daylight Time)

var d = new Date(); var n = d.toJSON();

var d = new Date(); var n = d.toJSON();

will get you the answer formatted the way you were looking for it.

将为您提供您正在寻找的格式的答案。

Here is a great explanation of all the ways to manipulate the Date object

这是对操作 Date 对象的所有方法的很好的解释