Python "2021-06-14T15:00:00.000Z" form question

Asked 2 years ago, Updated 2 years ago, 17 views

The date format I receive in response is "2021-06-15".

I have to put the date I received back in the request, but the format is

"2021-06-14T15:00.000Z" You have to change it like this and put it in (Because of UTC, there is a 9 hour difference, so that time comes out.)

I tried to put 14 on the "2021-06-14T15:00:00.000Z" Lee Hyung-sik's list by subtracting 1 from 15 as the first rank.

I'm asking you because I think there's another way.

Even if I search, I can only see "2021-06-14T15:00:00.000Z" and change this form to "2021-06-14."

I don't know how to do it backwards.

python

2022-09-20 15:43

1 Answers

ref :

>>> d = datetime.datetime.fromisoformat("2021-06-15")
>>> d
datetime.datetime(2021, 6, 15, 0, 0)
>>> d.astimezone(datetime.timezone.utc)
datetime.datetime(2021, 6, 14, 15, 0, tzinfo=datetime.timezone.utc)
>>> d.isoformat(timespec="milliseconds")
'2021-06-15T00:00:00.000'

>>> d = d.astimezone(datetime.timezone.utc)
>>> d
datetime.datetime(2021, 6, 14, 15, 0, tzinfo=datetime.timezone.utc)
>>> d.isoformat(timespec="milliseconds")
'2021-06-14T15:00:00.000+00:00'
>>> d.isoformat(timespec="milliseconds").replace("+00:00", "Z")
'2021-06-14T15:00:00.000Z'
>>> 
>>> def localYmd_to_isoformat_js(date_string: str):
    return (
        datetime.datetime.fromisoformat(date_string)
        .astimezone(datetime.timezone.utc)
        .isoformat(timespec="milliseconds")
        .replace("+00:00", "Z")
    )

>>> localYmd_to_isoformat_js("2021-06-15")
'2021-06-14T15:00:00.000Z'
>>> import datetime
>>> 
>>> (datetime.date.fromisoformat("2021-06-01") - datetime.timedelta(days=1)).strftime("%Y-%m-%d")
'2021-05-31'
>>> (datetime.date.fromisoformat("2021-06-01") - datetime.timedelta(days=1)).strftime("%Y-%m-%d") + "T15:00:00.000Z"
'2021-05-31T15:00:00.000Z'


2022-09-20 15:43

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.