I'm getting Java date format strings (yyyymmdd) as input. I need to convert them to Python-based format (%Y%m%d) or just use these to get the current date in that format in Python. e.g. I want to achieve the following in Python:
print(current_time.strftime('yyyymmdd')
Result:
20210426
Convert python date format (%Y) to java (yyyy)
Similar question but its the other way around and I can't use Template like this since there is no delimiter
I'm not sure I understand your question. Here's how to achieve the same output with python3 using datetime module:
import datetime
today_date_obj = datetime.date.today()
formatted_date_string = str(today_date_obj.strftime('%Y%m%d'))
print(formatted_date_string)
Dates in python are objects. To convert your date to string, use str().
Related
This is my data :
dates = np.arange("2018-01-01", "2021-12-31", dtype="datetime64[D]")
I now want to convert from :
"2018-01-01" -> "Jan-01-2018" ["Monthname-day-year"] format
How to i do this ?
Is it possible to initialize this in the way we want to convert ?
Can i use something like:
for i in dates:
i = i.replace(i.month,i.strftime("%b"))
You can try this:
from datetime import datetime
dates = np.arange("2018-01-01", "2021-12-31", dtype="datetime64[D]")
result_dates = []
for date in dates.astype(datetime):
result_dates.append(date.strftime("%b-%d-%Y"))
But you will need to convert result dates as shown in the code
I feel compelled to elaborate on Silvio Mayolo's very relevant but ostensibly ignored comment above. Python stores a timestamp as structure (see How does Python store datetime internally? for more information) Hence, the DateTime does not as such have a 'format'. A format only becomes necessary when you want to print the date because you must first convert the timestamp to a string. Thus, you do NOT need to initialise any format. You only need to declare a format when the time comes to print the timestamp.
While you CAN store the date as a string in your dataframe index in a specific format, you CANNOT perform time related functions on it without first converting the string back to a time variable. ie current_time.hour will return an integer with the current hour if current_time is a datetime variable but will crash if it is a string formatted as a timestamp (such as "2023-01-15 17:23").
This is important to understand, because eventually you will need to manipulate the variables and need to understand whether you are working with a time or a string.
How can I convert the following time format:
hhmmss.ff (like 110241.22 is 11:02:41.22)
into the date/time format with pandas?
I tries to use pandas.to_datetime() but it fails to do the conversion. Here is an example:
hhmmss='110241.22'
pd.to_datetime(hhmmss)
Thanks
You need to specify the format you want to convert the time to. Here's a helpful resource for figuring out what each symbol means. Here's Pandas documentation
pd.to_datetime(df['column_name'], format = '%H%M%S.%f')
I am using the .st_birthtime method to get the date of creation of a file.
The result looks like:
1359492652
which I can convert to a more readable format
2013-01-29 21:50:52
using
datetime.datetime.fromtimestamp(statinfo.st_birthtime)
My question is: how can I convert it to YYYYMMDD format? I don't give importance of the hours and minutes. In this example the result should be
20130129
Something like the SELECT CONVERT(VARCHAR(10), #date, 112) of T-SQL.
I am using Python version 3.5.3 and MacOS.
It's this what you wanted?
#time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime(1359492652))
time.strftime('%Y%m%d', time.gmtime(1359492652))
I've assumed that 1359492652 is the total of seconds so this is the right date formatters for Python, tested it in Python 3 interpreter. The first line which is a comment is the same result as you had with the datetime method.
if you want here is a link for the strftime behaviour: https://docs.python.org/3/library/datetime.html#strftime-strptime-behavior
I have a pandas dataframe with a column containing a date; the format of the original string is YYYY/DD/MM HH:MM:SS.
I am trying to convert the string into a datetime format, by using
df['Date']=pd.to_datetime(df['Data'], errors='coerce')
but plotting it I can see it doesn't recognize the correct format.
Can you help me to understand whether there is an option to give python the correct format to read the column?
I have seen the format tag for to_datetime function, but I can't use it correctly.
Thanks a lot for your help!
Try this:
df['Date'] = pd.to_datetime(df['Data'], format='%Y/%d/%m %H:%M:%S')
It looks like you're using a non-standard date format. It should be YYYY-MM-DD. Try formating with the strptime() method.
time.strptime('2016/15/07', '%Y/%d/%m')
If you need to get it to a string after that use time.strftime().
I have a bunch of human-readable dates and times (to be specific, the default format in nginx logs) that I wish to convert to Unix timestamps. The format is like this:
04/Dec/2013:18:56:05 +0000
What's the most reliable way of doing this? Are there any libraries I can use for this purpose?
I think you are looking for datetime.strptime.
From the doc link
The datetime.strptime() class method creates a datetime object from a string representing a date and time and a corresponding format string.
Call it like
datetime.strptime(date_string, format)