我有这种时间格式,它存储在数据库中。我想将其转换为jQuery countdown接受的格式。.我认为jQuery countdown接受这种格式。
Sun Jan 01 2012 00:00:00 GMT+0800 (Malay Peninsula Standard Time)
但是问题是,我的时间格式如下:
2011-03-29 00:01:03
为了让jQuery倒数进行倒数,我需要将其转换为长格式。
这是jQuery countdown的网站
最佳答案
您不需要那种长格式,也就是尝试打印Javascript Date对象时输出的格式。
您需要创建一个Java脚本Date object
这样做的本机方法如下:
var date = new Date([year], [month], [day]);
注意:月份是零索引。即一月为0,二月为1,十二月为11。
因此,如果您使用php吐痰。
$date = new DateTime('2011-03-29 00:01:03');
printf('var date = new Date(%d, %d, %d);',
$date->format('Y'),
$date->format('n') - 1,
$date->format('j'),
$date->format('H'),
$date->format('i'),
$date->format('s')
);
或者,您可以使用json传递它:
json_encode(array(
'year' => $date->format('Y'),
'month' => $date->format('n') - 1,
'day' => $date->format('j')
'hour' => $date->format('H'),
'minute' => $date->format('i'),
'second' => $date->format('s')
));
然后使用JavaScript创建日期:
var date = new Date(json.year, json.month, json.day,
json.hour, json.minute, json.second);