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)
Related
I have a string column in df which contains date in dd/MM/yyyy format and I want to convert that format to yyyy-MM-dd using with column
If you know you will have a consistent format in your column, you can pass this to 'to_datetime'.
You can try like below-
df['column_name'] = pd.to_datetime(df['column_name'], format='%d/%m/%y').dt.strftime('%Y-%m-%d')
Using python's datetime lib it can be done accordingly:
from datetime import datetime
date_string = '10/10/2000'
datetime_object = datetime.strptime(date_string, '%d/%m/%Y')
converted_date_string = datetime_object.strftime('%Y-%m-%d')
How can i get datetime in ISO 8601 date format (YYYY-MM-DDThh:mmTZD). Example 2019-02-26T09:30:46+03:00
I tried using
from datetime import datetime
d = datetime.now()
d.isoformat()
But the output is now correct
'2020-07-29T15:47:46.974744'
d = datetime.now()
d.strftime("%Y-%m-%dT%H:%M:%S%z")
Do note that %z will return empty if its a naive datetime object
This should give you the format you're looking for.
https://strftime.org/
This is a good reference on the available formats
I'm trying to normalize a date string: '6-3-1975' to a datetime object in this format: '06/03/1975'
I have this method:
def normalizeDate(date):
formatted_date = date.replace('-', '/')
date_obj = datetime.strptime(formatted_date, '%m/%d/%Y').date()
# date = datetime.strftime(date_obj, '%m/%d/%Y')
# print(date)
return date_obj
When printing out .date() the format uses dashes, I also tried using strftime but that would convert the date_object back into a string. Is there a way to reformat the date to use slashes and still be a datetime object?
You can process the date string yourself, to give it the desired format and then convert it to datetime object:
def normalizeDate(date):
newDate = '/'.join(str.zfill(elem,2) for elem in date.split('-'))
date_obj = datetime.strptime(newDate, '%m/%d/%Y').date()
return date_obj
In order to print your date to that specific format, you will have to use strftime to the new datetime object created:
inDate = '6-3-1975'
d = normalizeDate(inDate)
print(d.strftime('%m/%d/%Y'))
print(type(d))
This is the only way to set the format of a datetime object.
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')
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'