本文介绍了解析Javascript中的Python日期时间字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将Python生成的datetime字符串解析为Javascript Date对象.我走了最简单的路线:

I need to parse a Python-generated datetime string into a Javascript Date object. I went the simplest route:

在Python中:

dstring = str(mydate)

example dstring ='2012-05-16 19:20:35.243710-04:00'

example dstring = '2012-05-16 19:20:35.243710-04:00'

在Javascript中(带有 datejs 库):

In Javascript (with datejs library):

d = new Date(dstring);

这使我在Chrome中获得了正确的日期对象,但是在Firefox和Safari中(在Mac上)却出现了无效日期"错误.

This gets me a correct date object in Chrome, but an "Invalid Date" error in Firefox and Safari (on Mac).

推荐答案

您需要将字符串解析为JS Date. Date 构造函数还不够.也许您应该考虑使用诸如datejs之类的库:

You need to parse the string into a JS Date. The Date constructor is not enough. Maybe you should consider using a library such as datejs:

http://www.datejs.com/

Datejs使用诸如以下的有用方法扩展了 Date 对象:

Datejs extends the Date object with useful methods such as:

Date.parse('Thu, 1 July 2004 22:30:00');

不用说日期/时间格式是可自定义的.

Needless to say that date/time formats are customizable.

这篇关于解析Javascript中的Python日期时间字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-05 05:20