This question already has answers here:
Generate RFC 3339 timestamp in Python [duplicate]
(7 answers)
Closed 2 years ago.
I would like to convert today's date to below format in python
What I tried:
>>> import datetime
>>> d_date = datetime.datetime.now()
>>> reg_format_date = d_date.strftime("%Y-%m-%d %I:%M:%S %p")
>>> print(reg_format_date)
2020-08-04 06:40:52 PM
Expected format:
2017-10-18T04:46:53.553472514Z
can some one suggest please
Use utcnow() instead of now() to get the UTC time.
Use the isoformat() method. You'll need to add the trailing "Z" yourself.
In summary:
from datetime import datetime
reg_format_date = datetime.utcnow().isoformat() + "Z"
Here's how to get the current UTC time and convert to the ISO-8601 format (which is what your example shows). The timezone is hardcoded to Z.
import datetime
datetime.datetime.now(datetime.timezone.utc).replace(tzinfo=None).isoformat() + 'Z'
Related
This question already has answers here:
Converting date/time in YYYYMMDD/HHMMSS format to Python datetime
(3 answers)
How do I get the day of week given a date?
(30 answers)
Closed last month.
How do I convert 20230102 to Monday?
Using python, I need to accomplish this. I have a column of numbers in the format yyyymmdd.
Parse with strptime and format with strftime:
>>> from datetime import datetime
>>> n = 20230102
>>> datetime.strptime(str(n), "%Y%m%d").strftime("%A")
'Monday'
See strftime() and strptime() Format Codes for documentation of the % strings.
You can convert number string into weekday using "datetime" module
import datetime
def get_weekday(date):
date = datetime.datetime.strptime(date, '%Y%m%d')
return date.strftime('%A')
print(get_weekday('20230102'))
This is how you can achieve your desired output.
You can do it with weekday() method.
from datetime import date import calendar
my_date = date.today()
calendar.day_name[my_date.weekday()] #Friday
This question already has answers here:
Python UTC datetime object's ISO format doesn't include Z (Zulu or Zero offset)
(13 answers)
Closed 3 months ago.
from datetime import datetime
import pytz
# local datetime to ISO Datetime
iso_date = datetime.now().replace(microsecond=0).isoformat()
print('ISO Datetime:', iso_date)
This doesn't give me the required format i want
2022-05-18T13:43:13
I wanted to get the time like '2022-12-01T09:13:45Z'
The time format that you want is known as Zulu time format, the following code changes UTC to Zulu format.
Example 1
import datetime
now = datetime.datetime.now(datetime.timezone.utc)
print(now)
Output
#2022-12-01 10:07:06.552326+00:00
Example 2 (Hack)
import datetime
now = datetime.datetime.now(datetime.timezone.utc)
now = now.strftime('%Y-%m-%dT%H:%M:%S')+ now.strftime('.%f')[:4] + 'Z'
print(now)
Output
#2022-12-01T10:06:41.122Z
Hope this helps. Happy Coding :)
You can use datime's strftime function i.e.
current_datetime = datetime.now().replace(microsecond=0)
print(f'ISO Datetime: {current_datetime.strftime("%Y-%m-%dT%H:%M:%SZ")}')
This question already has answers here:
How do I parse an ISO 8601-formatted date?
(29 answers)
Closed 4 months ago.
I have this time here : 2017-08-05T05:21:10.6582942Z
And I want to convert it into %Y-%m-%d %H:%M:%S
I can do that using some funky methods such as :
date = "2017-08-05T05:21:10.6582942Z"
new_date = date[:11] + " " + date[12:][:-9]
But is there any way I can do something cleaner with datetime or some libraries made for this specific purpose ?
Using the datetime library with the strptime method (for parsing) and the strftime method (for formatting), this can be accomplished with no splits and limited slicing as:
from datetime import datetime as dt
date = '2017-08-05T05:21:10.6582942Z'
output = dt.strptime(date[:-2], '%Y-%m-%dT%H:%M:%S.%f').strftime('%Y-%m-%d %H:%M:%S')
Output:
'2017-08-05 05:21:10'
Note:
The slice is needed to remove the last two characters from the string date, as the %f (fractional seconds) formatter only accepts six decimal values, and your string contains seven decimal values.
Per the formatting documentation:
%f: Microsecond as a decimal number, zero-padded to 6 digits.
Start with importing datetime:
import datetime as dt
Convert string to datetime object:
date = "2017-08-05T05:21:10.6582942Z"
new_date = dt.datetime.strptime(date[:-2], "%Y-%m-%dT%H:%M:%S.%f") # -2 slice to since %f only accepts 6 digits.
Format datetime object as string:
format_date = dt.datetime.strftime(new_date, "%Y-%m-%d %H:%M:%S") # returns your format
However, looking at your code it feels your date is already formatted and you don't require the last .strftime() usage.
This question already has answers here:
Convert string "Jun 1 2005 1:33PM" into datetime
(26 answers)
Closed 7 years ago.
I know this has been asked a few times, but my scenario is a little different... The objective I need to accomplish is to convert a string of digits '20150425' (which happens to be a date), into a date format such as, '2015-04-25'. I need this because I am trying to compare date objects in my code, but have one variable type represented as a string.
Example below:
date = '20150425' ## want to convert this string to date type format
# conversion here
conv_date = '2015-04-25' ## format i want it converted into
Hope this is clear. Should not be difficult, just do not know how to do it.
This works
from datetime import datetime
date = '20150425'
date_object = datetime.strptime(date, '%Y%m%d')
date_object
>>> datetime.datetime(2015,4,25,0,0)
Assuming the date strings will always be 8 characters:
date = '20150425'
fdate = "{}-{}-{}".format(date[0:4], date[4:6], date[6:]) # 2015-04-25
Alternatively, you can go the "heavier" route and use the actual datetime class:
from datetime import datetime
date = '20150425'
dt = datetime.strptime(date, "%Y%m%d")
dt.strftime("%Y-%m-%d") # 2015-04-25
This question already has answers here:
Convert string "Jun 1 2005 1:33PM" into datetime
(26 answers)
Closed 8 years ago.
I have a date stored as a string:-
16/07/2014 13:00:00
I want to convert this into timestamp.
Also from timestamp to this format again.
Please suggest the best possible way to do this in python.
You can use datetime to handle combined dates and times. You could parse this string using datetime.strptime but then you'd have to manually select the formatting.
Alternatively you can use the dateutil package which has a parser which can intelligently guess the string format and return a datetime object, as shown below:
from dateutil import parser
s = '16/07/2014 13:00:00'
d = parser.parse(s)
print(d)
# 2014-07-16 13:00:00
print(type(d))
# datetime.datetime
The documentation to look into this deeper is here
The functions you are looking for are time.strptime(string[, format]) to go from string to timestamp, and then from timestamp to string is time.strftime(format[, t])
Here is an example for your format:
>>> from datetime import datetime
>>>
>>> date_object = datetime.strptime('16/07/2014 13:00:00', '%d/%m/%Y %H:%M:%S')
>>> print date_object
2014-07-16 13:00:00
The to go back to your format (I have used gmtime() to get the current time to show you can convert any datetime to your desired format)
>>> from time import gmtime, strftime
>>> date_string = strftime("%d/%m/%Y %H:%M:%S", gmtime())
>>> print date_string
17/09/2014 09:31:00
Your best bet is the datetime library: https://docs.python.org/2/library/datetime.html
import datetime
mytime='16/07/2014 13:00:00'
pythontime=datetime.datetime.strptime(mytime, '%d/%m/%Y %H:%M:%S')
stringtime=pythontime.strftime('%d/%m/%Y %H:%M:%S')
Enjoy!