简体   繁体   English

Javascript构造函数的Date()参数

[英]Javascript Date() parameters for constructor

Let's say I want to create a date-object for 2012 Sep 1st, 10:11:15 h. 假设我想为2012年9月1日,10:11:15 h创建一个日期对象。

I figured out: 我想通了:

past = new Date(2012,08,01,10,11,15);// works!
past = new Date('2012,08,01,10,11,15');// doesn't work.

The problem is, I want to use it in combination with a method: 问题是,我想将它与一个方法结合使用:

past = new Date(mypastformatfunc(mystring_to_format));

This gives me NaN. 这给了我NaN。 No valid Date-object created. 没有创建有效的日期对象。 I checked the return of the mypastformatfunc() and it seems I have the right format. 我检查了mypastformatfunc()的返回,看起来我的格式正确。 Is there any escaping problem regarding quotes? 报价有没有逃避问题? How can I get this to work? 我怎样才能让它发挥作用? It's really strange... Thanks. 真的很奇怪......谢谢。

EDIT SOLVED: Problem was it wasn't a one value but single parameters. EDIT已解决:问题是它不是一个值而是单个参数。 They can't be passed by a function's return at once.... 它们不能立即通过函数的返回传递....

Use the date string as the parameter to the constructor. 使用日期字符串作为构造函数的参数。

past = new Date('2012,08,01,10,11,15'.replace(/(\d+),(\d+),(\d+),(\d+),(\d+),(\d+)/, '$1/$2/$3 $4:$5:$6'));

function mypastformatfunc(str) {
  return str.replace(/(\d+),(\d+),(\d+),(\d+),(\d+),(\d+)/, '$1/$2/$3 $4:$5:$6')
}

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

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