Date(dateString)和新Date(dateString)之间的区别 [英] Difference between Date(dateString) and new Date(dateString)

查看:132
本文介绍了Date(dateString)和新Date(dateString)之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些代码尝试解析日期字符串。



当我执行 alert(Date(2010-08-17 12:09:36));
它正确地解析日期,一切正常,但我不能调用与 Date 相关联的方法,如 getMonth()



当我尝试:

  var temp = new Date(2010-08-17 12:09:36); 
alert(temp);

我收到无效日期错误。



任何关于如何用新的Date()解析2010-08-17 12:09:36的想法?

解决方案

p> Date()



这样你可以调用一个名为 Date() 。它不接受任何参数,并返回一个表示当前日期和时间的字符串。



新日期()



这样你就创建了一个新的Date实例。



您只能使用以下构造函数:

  new Date()//当前日期和时间
new日期(毫秒)// 1970/01/01以来的毫秒
新日期(dateString)
新日期(年,月,日,小时,分,秒,毫秒)

所以,不允许使用 2010-08-17 12:09:36 作为构造函数的参数。



请参阅 w3schools






编辑:新日期(dateString)使用以下格式之一:




  • 1975年10月13日11:13:00

  • 1975年10月13日11:13

  • 1975年10月13日


I have some code that tries to parse a date string.

When I do alert(Date("2010-08-17 12:09:36")); It properly parses the date and everything works fine but I can't call the methods associated with Date, like getMonth().

When I try:

var temp = new Date("2010-08-17 12:09:36");
alert(temp);

I get an "invalid date" error.

Any ideas on how to parse "2010-08-17 12:09:36" with new Date()?

解决方案

Date()

With this you call a function called Date(). It doesn't accept any arguments and returns a string representing the current date and time.

new Date()

With this you're creating a new instance of Date.

You can use only the following constructors:

new Date() // current date and time
new Date(milliseconds) //milliseconds since 1970/01/01
new Date(dateString)
new Date(year, month, day, hours, minutes, seconds, milliseconds)

So, use 2010-08-17 12:09:36 as parameter to constructor is not allowed.

See w3schools.


EDIT: new Date(dateString) uses one of these formats:

  • "October 13, 1975 11:13:00"
  • "October 13, 1975 11:13"
  • "October 13, 1975"

这篇关于Date(dateString)和新Date(dateString)之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆