简体   繁体   English

Javascript:将时间戳转换为人类可读的日期?

[英]Javascript: Convert timestamp to human readable date?

I'm simply trying to convert a timestamp back into a human readable date but what I get when converted is strange and wrong.我只是试图将时间戳转换回人类可读的日期,但是转换后得到的结果很奇怪而且是错误的。

This is how I save the timestamp:这是我保存时间戳的方式:

var timestamp = Number(new Date());
localStorag.setItem("mytimestamp", timestamp);

and this is how I get it back and convert it to readable date:这就是我取回它并将其转换为可读日期的方法:

var mydate = localStorag.getItem("mytimestamp");
var jsDate = new Date(mydate*1000);

alert(jsDate);

The jsDate is wrong and I don't understand what's causing it! jsDate是错误的,我不明白是什么原因造成的!

Could someone please advise on this?有人可以就此提出建议吗?

You can use the Number data type instead of date * 1000 to achieve this.您可以使用Number数据类型代替date * 1000来实现此目的。 See code example below:请参阅下面的代码示例:

// generate a timestamp
var timestamp = Number(new Date()) //1479895361931

Then然后

// get the date representation from the timestamp
var date = new Date(timestamp) // Wed Nov 23 2016 18:03:25 GMT+0800 (WITA)

Here is an easy solution using the toDateString() method:这是使用toDateString()方法的简单解决方案:

const date = new Date(timestamp).toDateString();
console.log(date);

This will return something like: Thu Jul 14 2016这将返回类似于: 2016 年 7 月 14 日星期四

The cause of the issue here is when you multiply the timestamp by 1000.这里问题的原因是当您将时间戳乘以 1000 时。

If you simply pass myDate to the Date constructor you should get the correct time -如果您只是将myDate传递给Date构造函数,您应该得到正确的时间 -

var timestamp = Number(new Date());
localStorage.setItem("mytimestamp", timestamp);
// ...
var mydateStr = localStorage.getItem("mytimestamp");
var myDate = Number(mydateStr); // convert the string back to a number
var jsDate = new Date(mydate);

It's true that Javascript deals with milliseconds but since you are generating the timestamp with Javascript and then reading it back also with Javascript, no conversion is needed - you can use the value as-is. Javascript 确实处理毫秒,但由于您使用 Javascript 生成时间戳,然后也使用 Javascript 将其读回,因此不需要转换 - 您可以按原样使用该值。


As was pointed out to me by @manish in the comments, the value stored in localStorage will be a string - remember to convert it back to a number before passing it to the Date constructor.正如@manish 在评论中向我指出的那样,存储在 localStorage 中的值将是一个字符串 - 请记住在将其传递给 Date 构造函数之前将其转换回数字。

Try using moment.js .尝试使用moment.js It add functions such as它添加了诸如

moment().format('MMMM Do YYYY, h:mm:ss a'); // November 23rd 2016, 12:03:36 pm
moment().format('dddd');                    // Wednesday
moment().format("MMM Do YY");               // Nov 23rd 16
moment().format('YYYY [escaped] YYYY');     // 2016 escaped 2016
moment().format();                          // 2016-11-23T12:03:36+02:00
var yourTimestamp = localStorag.getItem("mytimestamp");
jsDate = new Date(yourTimestamp);

jsDateValues = [
   jsDate.getFullYear(),
   jsDate.getMonth()+1,
   jsDate.getDate(),
   jsDate.getHours(),
   jsDate.getMinutes(),
   jsDate.getSeconds(),
];
alert(jsDateValues); //=> [2011, 3, 25, 23, 0, 0]

and you can use the baove values and print in which format you wnat eg dd/mm/yyyy并且您可以使用 baove 值并以您想要的格式打印,例如 dd/mm/yyyy

The problem here is that when you store something in localstorage its being stored as string '1479895747557' and not a number So when you get the timestamp back from localstorage its not a number its a string.这里的问题是,当你在 localstorage 中存储一些东西时,它被存储为字符串'1479895747557'而不是数字所以当你从 localstorage 取回时间戳时,它不是一个数字,而是一个字符串。 Though when you multiply it the value actually changes due to internal conversion it becomes 1479895747557000 and this represents a different date from the one you saved.尽管当您乘以它时,由于内部转换,该值实际上发生了变化,它变成了1479895747557000 ,这表示与您保存的日期不同。 So the solution would be to convert the string back to a number.所以解决方案是将字符串转换回数字。 Here is the code that works fine.这是工作正常的代码。

 var timestamp = Number(new Date()); localStorage.setItem("mytimestamp", timestamp); var mydate = localStorage.getItem("mytimestamp"); var jsDate = new Date(Number(mydate)); alert(jsDate);

Here is the code that does not performs the conversion back to number this will give you an Invalid Date.这是不执行转换回数字的代码,这将为您提供无效日期。

 var timestamp = Number(new Date()); localStorage.setItem("mytimestamp", timestamp); var mydate = localStorage.getItem("mytimestamp"); var jsDate = new Date(mydate); alert(jsDate);

FIDDLE小提琴

By installing moment module in a nodejs project:通过在 nodejs 项目中安装moment模块:

npm i --save moment

in a file.js file you can write:file.js文件中,您可以编写:

const moment = require('moment');

let timestamp = Number(new Date());
let myMoment = moment(timestamp).format("MMMM Do YYYY_HH-mm-ss.SSSS"); // February 28th 2020_01-06-34.0000

Here the documentation of moment .这里moment的文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM