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
Related
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().
In python, I want to find the date and time and put it inside of a string.
I have tried this:
example = datetime.datetime.now()
print(example)
However it returns the date with the miliseconds. What do I do if i don't want the miliseconds included. Just the date and time formatted like this:
YYYY-MM-DD 00:00:00
Try this:
import datetime
example = datetime.datetime.now()
print(example.strftime("%Y-%d-%m %H:%M:%S"))
You need to format the datetime output using this:
datetime.datetime.now().strftime('%H:%M:%S')
This will give you Hours:Minutes:Seconds
To get the format as per your requirements use:
datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
This will give you date time in YYYY-MM-DD 00:00:00 format without milliseconds.
My output: 2019-11-04 22:09:06
To see more directives: Link
Link says:
You can get the formatted date and time using strftime function. It
accepts a format string that you can use to get your desired output
If I understand your question corretly, somethinig like this should work for you.
from datetime import datetime
now = datetime.now()
date = now.strftime("%m/%d/%Y")
print("date:",date)
It came from here. There are other formatting examples in that post.
Datetime's strftime functionality does exactly what you're looking for.
There is some overview and explanation here:
https://www.programiz.com/python-programming/datetime/strftime
print(example.strftime('%Y-%m-%d %H:%M:%S')) will give you the right answer.
I would also look at the arrow library, as it offers a nice API to work with time.
I am using python to pull some records from sybase db and writing it in csv file but am receiving the date in the format JUL 22 12:00AM, while I need it to be in dd/mm/yyyy HH24:MI:SS format. Could you please suggest any workaround?
we cannot change the query which is pulling the records,so formatting the query will not work.
the data in table is in dd/mm/yyyy HH24:MI:SS format .
You can take a look at date.strptime() function:
from datetime import datetime
datetime.strptime("JUL 22 12:00AM", "%b %d %I:%M%p")
I also recommend you to take a look in other Stack Overflow questions which may help you:
how to convert a string date into datetime format in python?
How to convert string to datetime in python
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)
I'm reading a date from an Excel cell in Python (using .Value on the cell)... the result that I get is:
07/06/10 00:00:00
I thought this was a string, and so went about trying to figure out how to convert this to the format I need ("yyyyMMdd", or "20100706" in this example). However, after some playing around I realized that it is not being pulled as a string... Running type() on it returns <type 'time'> .
I then assumed that it was a Python time object, and tried using strftime on it to convert it to a string... but that didn't work either. It doesn't recognize the strftime method on the value.
Any idea on how to parse this properly and get the format I want? What am I doing wrong? (And if this clearly contains a date as well as a time, why is Python automatically considering it a time object?)
You can convert it to a time object like this:
import time
time.strptime(str(thetime), '%m/%d/%y %H:%M:%S')
And then you should be able to manipulate it to your hearts content.
HTH
Have you tried str(time)? That should give it to you in a string and then you can play around with the formatting all you like.