jQuery jquery将数字转换为日期?

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

jquery convert number to date?

jquerytimeepoch

提问by Patrioticcow

i have a json file that returns "date_created":"1273185387"in epoch format

我有一个"date_created":"1273185387"以纪元格式返回的 json 文件

i want to convert it to something like this Thu, 06 May 2010 22:36:27 GMT

我想把它转换成这样的 Thu, 06 May 2010 22:36:27 GMT

any script to do this conversion?

任何脚本来做这个转换?

回答by Matt Ball

var myObj = $.parseJSON('{"date_created":"1273185387"}'),
    myDate = new Date(1000*myObj.date_created);

console.log(myDate.toString());
console.log(myDate.toLocaleString());
console.log(myDate.toUTCString());

http://jsfiddle.net/mattball/8gvkk/

http://jsfiddle.net/mattball/8gvkk/

回答by Sangeet Menon

Try the below code...

试试下面的代码...

    var myDate = new Date( your epoch date *1000);
    alert(myDate.toGMTString());
    var mytime=myDate.toGMTString()

回答by Dustin Laine

alert(new Date(1273185387).toUTCString());

回答by T.J. Crowder

jQuery doesn't have anything for it, but that's okay, because JavaScript does. The Dateconstructor accepts a milliseconds-since-the-Epoch value, so in your case (since that looks like a seconds value) it would be:

jQuery 没有任何东西,但没关系,因为 JavaScript 有。该Date构造函数接受一个毫秒,因为最大纪元值,所以你的情况(因为这看起来像一个秒值),这将是:

var dt = new Date(obj.date_created * 1000);

...where objis the result of deserializing that JSON string.

...obj反序列化该 JSON 字符串的结果在哪里。

Details in Section 15.9.3.2 of the specification. Alternately, the MDC pageis useful.

规范第 15.9.3.2 节中的详细信息。或者,MDC 页面很有用。

回答by pepkin88

http://jsfiddle.net/y3Syc/1/

http://jsfiddle.net/y3Sys/1/

var data = {"date_created":"1273185387"};
var date = new Date(parseInt(data.date_created, 10) * 1000);
// example representations
alert(date);
alert(date.toLocaleString());

回答by Willie Cheng

Convert json date to date format in jQuery

将 json 日期转换为 jQuery 中的日期格式

 <script>
  var date = "\/Date(1297246301973)\/";
  var nowDate = new Date(parseInt(date.substr(6)));
  alert(nowDate )
</script>