如何通过json和twig将DateTime对象传递给javascript日期对象
例如,我有一个对象,它在php中具有如何通过json和twig将DateTime对象传递给javascript日期对象,javascript,php,json,datetime,twig,Javascript,Php,Json,Datetime,Twig,例如,我有一个对象,它在php中具有DateTime对象 php中的 array_push($events, array( "date" => new \DateTime('2017-08-01'), "description" => "This is description of an event" )); array_push($events, array( "date" =>
DateTime
对象
php中的
array_push($events,
array(
"date" => new \DateTime('2017-08-01'),
"description" => "This is description of an event"
));
array_push($events,
array(
"date" => new \DateTime('2017-07-19'),
"description" => "Some longer\ntext can also\n be added"
));
像这样解析对象
在树枝中
{% for var, value in events %}
var {{var}} = {{ value|json_encode|raw }};
{% endfor %}
输出
var 0 = {"date":{"date":"2017-08-01 00:00:00.000000","timezone_type":3,"timezone":"Asia\/Tokyo"},"description":"This is description of an event"};
var 1 = {"date":{"date":"2017-07-19 00:00:00.000000","timezone_type":3,"timezone":"Asia\/Tokyo"},"description":"Some longer\ntext can also\n be added"};
这样,text
和boolean
工作正常,但无法将DateTime转换为javascript对象
有什么好的解决方案吗???您应该能够使用JSON中的datetime字符串来实例化一个
Date
对象,如下所示:
var d = new Date('2017-08-01 00:00:00.000000');
然后可以这样使用,例如:
alert(d.toString()); //Tue Aug 01 2017 00:00:00 GMT+0100 (GMT Summer Time)
谢谢,我发现Date类构造函数可以使用多种字符串。