javascript 将日期转换为长格式,它是如何工作的?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19114614/
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
Conversion of date into long format, How it works?
提问by agpt
I was trying to convert date object into long format (may be in milliseconds format) as we do in java.
So to fulfill my need, after some trial and error, I found below way which works for me:
我试图将日期对象转换为长格式(可能是毫秒格式),就像我们在 java 中所做的那样。
因此,为了满足我的需要,经过一些反复试验,我发现以下方法对我有用:
var date = new Date();
var longFormat = date*1; // dont know what it does internally
console.log(longFormat); // output was 1380625095292
To verify, I reverse it using new Date(longFormat);
and it gave me correct output. In short I was able to fulfill my need some how, but I am still blank what multiplication does internally ? When I tried to multiply current date with digit 2, it gave me some date of year 2057 !! does anyone know, what exactly happening ?
为了验证,我使用反转它new Date(longFormat);
,它给了我正确的输出。简而言之,我能够以某种方式满足我的需求,但我仍然对乘法在内部做什么感到空白?当我尝试将当前日期与数字 2 相乘时,它给了我 2057 年的某个日期!!有谁知道,究竟发生了什么?
回答by Artyom Neustroev
The long format displays the number of ticksafter 01.01.1970, so for now its about 43 years.
长格式显示01.01.1970之后的刻度数,所以现在大约是 43 年。
*
operator forces argument to be cast to number, I suppose, Date
object has such casting probably with getTime()
.
*
运算符强制参数强制转换为数字,我想,Date
对象可能具有这样的转换getTime()
。
You double the number of milliseconds - you get 43 more years, hence the 2057 (or so) year.
您将毫秒数增加一倍 - 又多了 43 年,因此是 2057(左右)年。
回答by Paritosh
What you are getting when you multiply, is ticks
当你乘法时,你得到的是滴答声
Visit: How to convert JavaScript date object into ticks
Also, when you * 2
it, you get the double value of ticks, so the date is of future
此外,当你使用* 2
它时,你会得到双倍的刻度值,所以日期是未来的
var date = new Date()
var ticks = date.getTime()
getTime
returns the number of milliseconds since January 1, 1970. So when you * 1
it, you might have got value of this milliseconds. When you * 2
it, those milliseconds are doubled, and you get date of 2057!!
getTime
返回自 1970 年 1 月 1 日以来的毫秒数。因此,当您使用* 1
它时,您可能已经获得了这个毫秒的值。当你使用* 2
它时,这些毫秒会加倍,你会得到 2057 年的日期!!
回答by naloxx
Dates are internally stored as a timestamp, which is a long-object (more info on timestamps). This is why you can create Dates with new Date(long). If you try to multiply a Date with an integer, this is what happens:
日期在内部存储为时间戳,这是一个长对象(有关时间戳的更多信息)。这就是为什么您可以使用 new Date(long) 创建日期。如果您尝试将 Date 与整数相乘,则会发生以下情况:
var date = new Date();
var longFormat = date*1;
// date*1 => date.getTime() * 1
console.log(longFormat); // output is 1380.....
Javascript tries to find the easiest conversion from date to a format that can be multiplied with the factor 1, which is in this case the internal long format
Javascript 试图找到从日期到可以乘以因子 1 的格式的最简单的转换,在这种情况下是内部长格式
回答by Norbert Pisz
Just use a date object methods.
只需使用日期对象方法。
Read the docs: JavaScript Date object
阅读文档:JavaScript Date 对象
var miliseconds=yourDateObject.getMiliseconds();
If You want to get ticks:
如果你想得到蜱:
var ticks = ((yourDateObject.getTime() * 10000) + 621355968000000000);
or
或者
var ticks = someDate.getTime();
回答by RobG
Javascript date objects are based on a UTC time valuethat is milliseconds since 1 January 1970. It just so happens that Java uses the same epoch but the time value is seconds.
Javascript 日期对象基于 UTC时间值,该值是自 1970 年 1 月 1 日以来的毫秒数。碰巧 Java 使用相同的纪元但时间值是秒。
To get the time value, the getTimemethod can be used, or a mathematic operation can be applied to the date object, e.g.
获取时间值,可以使用getTime方法,也可以对日期对象进行数学运算,例如
var d = new Date();
alert(d.getTime()); // shows time value
alert(+d); // shows time value
The Date constructor also accepts a time value as an argument to create a date object, so to copy a date object you can do:
Date 构造函数还接受时间值作为参数来创建日期对象,因此要复制日期对象,您可以执行以下操作:
var d2 = new Date(+d);
If you do:
如果你这样做:
var d3 = new Date(2 * d);
you are effectively creating a date that is (very roughly):
您正在有效地创建一个日期(非常粗略):
1970 + (2013 - 1970) * 2 = 2056
回答by ch.smrutiranjan parida
You could try the parsing functionalityof the Date constructor, whose result you then can stringify:
您可以尝试Date 构造函数的解析功能,然后您可以将其结果字符串化:
>
>
new Date("04/06/13").toString()
"Sun Apr 06 1913 00:00:00 GMT+0200"
// or something But the parsing is implementation-dependent, and there won't be many engines that interpret your odd DD/MM/YY format correctly. If you had used MM/DD/YYYY, it probably would be recognized everywhere.
// 或者别的什么但是解析是依赖于实现的,并且不会有很多引擎正确解释你奇怪的 DD/MM/YY 格式。如果您使用过 MM/DD/YYYY,它可能会在任何地方被识别。
Instead, you want to ensure how it is parsed, so have to do it yourself and feed the single parts into the constructor:
相反,您想确保它是如何解析的,因此必须自己做并将单个部分提供给构造函数:
var parts = "04/06/13".split("/"),
date = new Date(+parts[2]+2000, parts[1]-1, +parts[0]);
console.log(date.toString()); // Tue Jun 04 2013 00:00:00 GMT+0200