admin管理员组文章数量:1291050
I'm trying to convert a string which represents a date in a milliseconds format like this :
var start = new Date(s);
However, it seems that it does not work because when I'm trying to display the date, I've got "Invalid date" as an error message.
What I would like to get is a date in this format :
Wed May 07 2014 09:00:00
Is this possible to do?
EDIT : The original value of the s variable is a string posed of 13 number (for instance : 13982762900000)
I'm trying to convert a string which represents a date in a milliseconds format like this :
var start = new Date(s);
However, it seems that it does not work because when I'm trying to display the date, I've got "Invalid date" as an error message.
What I would like to get is a date in this format :
Wed May 07 2014 09:00:00
Is this possible to do?
EDIT : The original value of the s variable is a string posed of 13 number (for instance : 13982762900000)
Share Improve this question edited May 7, 2014 at 13:16 j08691 208k32 gold badges269 silver badges280 bronze badges asked May 7, 2014 at 13:07 TraffyTraffy 2,86115 gold badges54 silver badges80 bronze badges 5- Is it possible to make a live demo? – R3tep Commented May 7, 2014 at 13:08
-
2
what is the content of
s
like? – Aditya Jain Commented May 7, 2014 at 13:09 -
Can you give an example of the
s
variable? – tar Commented May 7, 2014 at 13:09 -
1
What is the value of
s
? – Rory McCrossan Commented May 7, 2014 at 13:09 - @Traffy With jsfiddle for example – R3tep Commented May 7, 2014 at 13:10
3 Answers
Reset to default 6Convert it to a numeric type instead of a string:
var date = new Date(parseInt(s, 10))
Explanation:
The input to the new Date()
constructor is a string. This means new Date()
will assume the input is "a ISO8601 string" instead of "Integer value representing the number of milliseconds", as described below.
According to https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date it should be an integer if the value shall be interpreted as "representing the number of milliseconds since 1 January 1970 00:00:00 UTC (Unix Epoch)."
new Date()
has the following constructors (according to the link above):
new Date();
new Date(value);
new Date(dateString);
new Date(year, month, day, hour, minute, second, millisecond);
value (this is the constructor being used if you convert it to an integer value)
Integer value representing the number of milliseconds since 1 January 1970 00:00:00 UTC (Unix Epoch).
dateString (this was the constructor being called before)
String value representing a date. The string should be in a format recognized by the Date.parse() method (IETF-pliant RFC 2822 timestamps and also a version of ISO8601).
var date = new Date(parseInt(your_timestamp, 10));
A timestamp should have 13 digits.
Your example timestamp has 14 digits. Is that a mistake or the timestamp is actually wrong?
You could:
var date = new Date(parseInt(your_timestamp, 10) / 10);
Transform your string in integer with parseInt
and it's working :
var start = new Date(parseInt(s, 10));
Live Demo
Reference
- MDN parseInt()
- MDN Date Object
本文标签: javascriptConverting a string representing milliseconds to a DateStack Overflow
版权声明:本文标题:javascript - Converting a string representing milliseconds to a Date - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741515533a2382863.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论