什么是Javascript的'Date.prototype.toISOString`的Python等价物?

时间:2021-10-03 20:25:55

In Javascript, Date.prototype.toISOString gives an ISO 8601 UTC datetime string:

在Javascript中,Date.prototype.toISOString给出了ISO 8601 UTC日期时间字符串:

new Date().toISOString()
// "2014-07-24T00:19:37.439Z"

Is there a Python function with behavior that matches Javascript's?

是否有一个Python函数的行为与Javascript相匹配?


Attempts:

Python's datetime.datetime.isoformat is similar, but not quite the same:

Python的datetime.datetime.isoformat类似,但不完全相同:

datetime.datetime.now().isoformat()
// '2014-07-24T00:19:37.439728'

Using pytz I can at least make UTC explicit:

使用pytz我至少可以使UTC显式:

pytz.utc.localize(datetime.now()).isoformat())
// '2014-07-24T00:19:37.439728+00:00'

4 个解决方案

#1


2  

I attempted to format the string to exactly how it is in the javascript output.

我试图将字符串格式化为javascript输出中的字符串。

from datetime import datetime

def iso_format(dt):
    try:
        utc = dt + dt.utcoffset()
    except TypeError as e:
        utc = dt
    isostring = datetime.strftime(utc, '%Y-%m-%dT%H:%M:%S.{0}Z')
    return isostring.format(int(round(utc.microsecond/1000.0)))

print iso_format(datetime.now())
#"2014-07-24T00:19:37.439Z"

#2


0  

You can use this code:

您可以使用此代码:

import datetime
now = datetime.datetime.now()
iso_time = now.strftime("%Y-%m-%dT%H:%M:%SZ") 

#3


0  

Is there a Python function with behavior that matches Javascript's?

是否有一个Python函数的行为与Javascript相匹配?

Not in the standard library, but you could build your own.

不在标准库中,但您可以构建自己的库。

#4


0  

# Used dateutil package from https://pypi.org/project/python-dateutil/
import datetime
import dateutil.tz

def iso_format(dt):
    try:
        utc_dt = dt.astimezone(dateutil.tz.tzutc())
    except ValueError:
        utc_dt = dt
    ms = "{:.3f}".format(utc_dt.microsecond / 1000000.0)[2:5]
    return datetime.datetime.strftime(utc_dt, '%Y-%m-%dT%H:%M:%S.{0}Z'.format(ms))

#1


2  

I attempted to format the string to exactly how it is in the javascript output.

我试图将字符串格式化为javascript输出中的字符串。

from datetime import datetime

def iso_format(dt):
    try:
        utc = dt + dt.utcoffset()
    except TypeError as e:
        utc = dt
    isostring = datetime.strftime(utc, '%Y-%m-%dT%H:%M:%S.{0}Z')
    return isostring.format(int(round(utc.microsecond/1000.0)))

print iso_format(datetime.now())
#"2014-07-24T00:19:37.439Z"

#2


0  

You can use this code:

您可以使用此代码:

import datetime
now = datetime.datetime.now()
iso_time = now.strftime("%Y-%m-%dT%H:%M:%SZ") 

#3


0  

Is there a Python function with behavior that matches Javascript's?

是否有一个Python函数的行为与Javascript相匹配?

Not in the standard library, but you could build your own.

不在标准库中,但您可以构建自己的库。

#4


0  

# Used dateutil package from https://pypi.org/project/python-dateutil/
import datetime
import dateutil.tz

def iso_format(dt):
    try:
        utc_dt = dt.astimezone(dateutil.tz.tzutc())
    except ValueError:
        utc_dt = dt
    ms = "{:.3f}".format(utc_dt.microsecond / 1000000.0)[2:5]
    return datetime.datetime.strftime(utc_dt, '%Y-%m-%dT%H:%M:%S.{0}Z'.format(ms))