How I do convert from timestamp to date in python? - python

I have this string '2015-04-08T07:52:00Z' and I wanna to convert it to '08/04/2015', how can I do this?

You can use the datetime.datetime.strptime() function to create a datetime object, then datetime.datetime.strftime() to return your correctly formatted date like so:
from datetime import datetime
dt = datetime.strptime('2015-04-08T07:52:00Z', '%Y-%m-%dT%H:%M:%SZ')
print dt.strftime('%d/%m/%Y')

You can use easy_date to make it easy:
import date_converter
converted_date = date_converter.string_to_string('2015-04-08T07:52:00Z', '%Y-%m-%dT%H:%M:%SZ', '%d/%m/%Y')

Related

Python: convert date format YYYY-mm-dd to dd-MON-yyyy with abbreviated month

I have a date that is a string in this format:
'2021-01-16'
And need to convert it to a string in this format:
'16-JAN-2021'
I am able to get most of it like this:
x = datetime.strptime('2021-01-16', '%Y-%m-%d')
x.strftime('%d-%b-%Y')
But the month is not fully capitalized:
'16-Jan-2021'
Just use upper() to capitalize the output string:
from datetime import datetime
x = datetime.strptime('2021-01-16', '%Y-%m-%d')
print(x.strftime('%d-%b-%Y').upper())
# 16-JAN-2021
You were almost there. Simply use upper().
>>> from datetime import datetime
>>> datetime.strptime('2021-01-16', '%Y-%m-%d').strftime('%d-%b-%Y').upper()
'16-JAN-2021'
x.strftime('%d-%b-%Y').upper()
I read answers with upper() function, here is another way using %^b
from datetime import datetime
date = datetime.strptime('2011-01-16', '%Y-%m-%d')
formatted_date = date.strftime('%d-%^b-%Y')
print(formatted_date)
Goodluck!

Convert Timestamp to ISO Date

I am extracting Data from Mongodb using some date filter. In mongo my date is in ISO format . As i am dynamically adding date from some variable which is in timestamp format(2019-07-15 14:54:53).Getting Empty Result
curs = col1.aggregate([{'$match':{update_col: {'$gte': last_updt }}},{'$project':json_acceptable_string}])
I am expecting Rows after filtering but acual its giving empty dataset
you can use datetime.strptime to parse the original string to a datetime object, then use datetime.isoformat to get it in ISO format.
try this:
import datetime
original_date = '2019-07-15 14:54:53'
date_obj = datetime.datetime.strptime(original_date, "%Y-%m-%d %H:%M:%S")
iso_date = date_obj.isoformat()
print(iso_date)
try this
from dateutil import parser as date_parser
dt_obj = date_parser.parse('2019-07-15 14:54:53')
where dt_obj is an object of standard datetime.datetime class
You can use fromisoformat.
Try
from datetime import datetime
iso_string = '2019-07-15 14:54:53'
you_date_obj = datetime.fromisoformat(iso_string)

Modify datetime.now() standard output

I what to change standard time output to "%Y-%m-%dT%H:%M:%S.%f+00:00" format, how can I do it?
Here is what i have now.
import datetime
start_date = datetime.datetime.now()
print(start_date,"+00:00")
#output: 2019-04-19 06:57:47.563791 +00:00
#what to be: 2019-04-19T06:57:47.563791+00:00
Use exactly that format string with datetime.strftime:
datetime.now().strftime("%Y-%m-%dT%H:%M:%S.%f+00:00")
Use datetime.strftime to convert your datetime object to the required format
import datetime
start_date = datetime.datetime.now()
print(start_date.strftime("%Y-%m-%dT%H:%M:%S.%f+00:00"))
#2019-04-26T12:50:45.219062+00:00

Python convert date to datetime

I have following string of date "2018-05-08" and I would like to convert it into the datetime format of python like: "2018-05-08T00:00:00.0000000". I understand I can combine the string like:
> date = "2018-05-08"
> time = "T00:00:00.0000000"
> date+time
'2018-05-08T00:00:00.0000000'
But is there pythonic way of doing it where I can use libraries like datetime?
You can use datetime module like this example:
import datetime
date = "2018-05-08"
final = datetime.datetime.strptime(date, '%Y-%m-%d').strftime('%Y-%m-%dT%H:%M:%S.%f')
print(final)
Output:
2018-05-08T00:00:00.000000
For more details visit datetime's documentation
Use this code
from datetime import date
from datetime import datetime
d = date.today()
datetime.combine(d, datetime.min.time())
from datetime import datetime
datetime.strftime(datetime.strptime('2018-05-08','%Y-%m-%d'),'%Y-%m-%dT%H:%M:%S.%f')
OUTPUT:
'2018-05-08T00:00:00.000000'
from datetime import datetime
date_as_datetime = datetime.strptime("2018-05-08", '%Y-%m-%d')
date_as_string = date_as_datetime.strftime('%Y-%m-%dT%H:%M:%S')
print(date_as_string)
Out:
'2018-05-08T00:00:00'
See strptime/strftime options here: https://docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior
Yes, you can use the datetime module and the strftime method from that module. Here is an example below if we want to format the current time.
import datetime
current_time = datetime.datetime.now()
formatted_time = datetime.datetime.strftime(current_time, "%Y-%h-%d %H:%m:%S")
print(formatted_time)
Here is the output:
2018-May-08 13:05:16

How to convert a time to a string

I am using the date time lib to get the current date. After obtaining the current date I need to convert the obtained date to in to a string format. Any help is appriciated
from datetime import date
today=date.today()
print today
You can use today.strftime(format). format will be a string as described here http://docs.python.org/library/time.html#time.strftime . Example:
from datetime import date
today = date.today()
today.strftime("%x")
#>>> '01/31/11'
datetime.strftime allows you to format a datetime however you want
stringDate = str(today)
If that's what you want.
In Python3, you can use f-string formatting:
from datetime import date
today=date.today()
today_str = f"{today:%m/%d/%y}"
today_str
#>>>'06/24/22'

Categories

Resources