This is a basic question but am getting tangled up
I have a string variable referencePeriodEndDate which contains a date with type string
Which I am trying to convert to a date only format
so '31/3/2017' to 2017-03-31
But am getting stuck. I've so far tried to use:
datetimeobject = datetime.strptime(referencePeriodEndDate,'%Y-%m-%d')
datetimeobject = referencePeriodEndDate.strftime('%Y-%m-%d')
if you can use the dateutil module
from dateutil import parser
dt = parser.parse("31/3/2017")
print dt.strftime('%Y-%m-%d')
Output:
2017-03-31
Using datetime
import datetime
A = datetime.datetime.strptime('31/3/2017','%d/%m/%Y')
print A.strftime('%Y-%m-%d')
Related
I have a date string with the format '12/Sep/21'. I want to change the format to '2021-09-12'. How can I do this in Python?
I tried this:
dte1 = '02/Sep/21'
dte2 = datetime.datetime.strptime(dte1, '%d %b %y').strftime('%d/%m/%Y')
dte2
You may mean by:
import datetime
dte1 = '02/Sep/21'
dte2 = datetime.datetime.strptime(dte1, '%d/%b/%y').strftime('%Y-%m-%d')
print(dte2)
Output:
2021-09-02
It’s so easy to convert a date format into your desired date format by using the datetime package of Python:
import datetime
your_date = '02/Sep/21'
desire_format = datetime.datetime.strptime(your_date, '%d/%b/%y').strftime('%Y-%m-%d')
print(desire_format)
Output
2021-09-02
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)
I have a date column in my dataframe that consists of strings like this...'201512'
I would like to convert it into a datetime object of just year to do some time series analysis.
I tried...
df['Date']= pd.to_datetime(df['Date'])
and something similar to
datetime.strptime(Date, "%Y")
I am not sure how datetime interfaces with pandas dataframes (perhaps somebody will comment if there is special usage), but in general the datetime functions would work like this:
import datetime
date_string = "201512"
date_object = datetime.datetime.strptime(date_string, "%Y%m")
print(date_object)
Getting us:
2015-12-01 00:00:00
Now that the hard part of creating a datetime object is done we simply
print(date_object.year)
Which spits out our desired
2015
More info about the parsing operators (the "%Y%m" bit of my code) is described in the documentation
I would look at the module arrow
https://arrow.readthedocs.io/en/latest/
import arrow
date = arrow.now()
#example of text formatting
fdate = date.format('YYYY')
#example of converting text into datetime
date = arrow.get('201905', 'YYYYMM').datetime
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!
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'