我在JavaScript中使用以下格式的日期字符串:
"2014-04-10T08:35:18.648Z"
我想将其更改为:
"2014-04-10 08:35:18.648"
我该怎么做?
答案 0 :(得分:0)
<强> PHP 强>
在PHP中,你可以做一些简单的事情:
$date = '2014-04-10T08:35:18.648Z';
$date = str_replace(array('T', 'Z'), array(' ', ''), $date);
// var_dump($date);
// string(23) "2014-04-10 08:35:18.648"
或者如果您更喜欢更复杂的方法:
$date = '2014-04-10T08:35:18.648Z';
preg_match('/^(?P<year>\d{4})-(?P<month>\d{2})-(?P<day>\d{2})T(?P<hours>\d{2}):(?P<minutes>\d{2}):(?P<seconds>\d{2})\.(?P<ms>\d+)Z$/i', $date, $m);
$date = sprintf('%d-%02d-%02d %02d:%02d:%02d.%d', $m['year'], $m['month'], $m['day'], $m['hours'], $m['minutes'], $m['seconds'], $m['ms']);
// var_dump($date);
// string(23) "2014-04-10 08:35:18.648"
<强> JAVASCRIPT 强>
在javascript中它更容易:
var dt = '2014-04-10T08:35:18.648Z';
dt = dt.replace('T', ' ').replace('Z', '');
// console.info(dt);
// "2014-04-10 08:35:18.648"