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
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().
I have data which is in-64 in the Index with values like "01/11/2018" in the index. It is data that has been imported from a csv. I am unable to convert it to a "01-11-2018" format. How do I do this because I get an error message:
'time data 0 does not match format '%Y' (match)'
I got the data from the following website:
https://www.nasdaq.com/symbol/spy/historical
and you can find a ' Download this file in Excel Format ' icon at the bottom.
import datetime
spyderdat.index = pd.to_datetime(spyderdat.index, format='%Y')
spyderdat.head()
How do I format this correctly?
Thanks a lot.
Your format string must match exactly:
import datetime
spyderdat.index = pd.to_datetime(spyderdat.index, format='%d/%m/%Y')
spyderdat.head()
Example w/o spyder:
import datetime
date = "1/11/2018"
print(datetime.datetime.strptime(date,"%d/%m/%Y"))
Output:
2018-11-01 00:00:00
You can strftime this datetime then anyhow you like. See link for formats. Or you store datetimes.
Assuming your input is a string, simply converting the / to - won't fix the issue.
The real problem is that you've told to_datetime to expect the input string to be only a 4-digit year but you've handed it an entire date, days and months included.
If you meant to use only the year portion you should manually extract the year first with something like split.
If you meant to use the full date as a value, you'll need to change your format to something like %d/%m/%Y. (Although I can't tell if your input is days first or months first due to their values.)
The easy way is to try this
datetime.datetime.strptime("01/11/2018", '%d/%m/%Y').strftime('%d-%m-%Y')
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 pandas column row['date'] which contains date in format 11/05/2015. I am trying to insert it into mysql db but having problems due to incorrect format of date field data. It has to be converted into 2015-11-05 in order to be inserted. Without storing the new value in variable how can I convert the date into required format?
Current format: 11/05/2015
Required format: 2015-11-05
Is the current format mm/dd/yyyy? If so
from datetime import datetime
row['date'] = datetime.strptime(row['date'], '%m/%d/%Y').strftime('%Y-%m-%d')
Use dateutil.parser,
This module offers a generic date/time string parser which is able to parse most known formats to represent a date and/or time.
Here is a MWE.
from dateutil.parser import parse
current_date = '11/05/2015'
required_date = parse(current_date).strftime('%Y-%m-%d')
PS: to explicitly distinguish between DM and MD, pass the argument dayfirst=True/False to parse, i.e. dayfirst=True represents DM and dayfirst=False represents MD.
This should do the job, w/o needing datetime:
"{2}-{0}-{1}".format(*(original_date.split("/")))
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)