Django date format issue - does not match format - python

I need to change the date into the format MM/YY. The issue is I don't always don't what the date format is to begin with. It's my understanding that the code below would need to know what format the date was in first before I can change it is that correct?
newdate = datetime.datetime.strptime(str(mydate), '%Y/%m/%d').strftime('%m-%Y')
as a result I get the error:
time data '2020-09-30' does not match format '%Y/%m/%d' (
How to I convert my date to MM/YY?
I'm using Django 1.4.6

newdate = datetime.datetime.strptime(str(mydate), '%Y-%m-%d').strftime('%m/%Y')

Related

What's the correct datetime format for this string date generated by python?

I have this date example '2022-08-30T11:53:52.204219' stored in database, when I get it from database it's type is string so I wanted to convert it to a date type by using this python code
datetime.strptime('2022-08-30T11:53:52.204219', "%Y-%m-%d'T'%H:%M:%S.%f")
I also tried this one
datetime.strptime('2022-08-30T11:53:52.204219', "yyyy-MM-dd'T'HH:mm:ssZ")
But I always get this error response 'time data '2022-08-30T11:53:52.204219' does not match format "%Y-%m-%d'T'%H:%M:%S.%f'
I need help to convert this string date to an actual date
As per comment:
from datetime import datetime
print(datetime.strptime('2022-08-30T11:53:52.204219', "%Y-%m-%dT%H:%M:%S.%f"))
Result:
2022-08-30 11:53:52.204219

Pyspark - Convert to Timestamp

Spark version : 2.1
I'm trying to convert a string datetime column to utc timestamp with the format yyyy-mm-ddThh:mm:ss
I first start by changing the format of the string column to yyyy-mm-ddThh:mm:ss
and then convert it to timestamp type. Later I would convert the timestamp to UTC using to_utc_timestamp function.
df.select(
f.to_timestamp(
f.date_format(f.col("time"), "yyyy-MM-dd'T'HH:mm:ss"), "yyyy-MM-dd'T'HH:mm:ss"
)
).show(5, False)
The date_format works fine by giving me the correct format. But, when I do to_timestamp on top of that result, the format changes to yyyy-MM-dd HH:mm:ss, when it should instead be yyyy-MM-dd'T'HH:mm:ss. Why does this happen?
Could someone tell me how I could retain the format given by date_format? What should I do?
The function to_timestamp returns a string to a timestamp, with the format yyyy-MM-dd HH:mm:ss.
The second argument is used to define the format of the DateTime in the string you are trying to parse.
You can see a couple of examples in the official documentation.
The code should be like this, just look at the single 'd' part here, and this is tricky in many cases.
data= data.withColumn('date', to_timestamp(col('date'), 'yyyy/MM/d'))

What kind of timestamp is this? Wrong conversion

I'm reading a file in python and I;m trying to convert the timestamp field into a readable form.
My timestamps have the following form:
174bb26825e
174bb26f5b6
174bb2641d2
174bb2705db
174bb26f390
When I use datetime.datetime.utcfromtimestamp I get a date that is in 1970 and when I convert it online in a Unix Hex Timestamp Converter I get a date that comes from the future(52699)!
My code is the following:
df['DateTime'] = df['DateTime'].apply(lambda x: datetime.datetime.utcfromtimestamp(float(int(x, 16))/16**4))
Any ideas?

How to show date value in another format (python date to text) on worksheet

I am using the Google Sheets API for inserting some data on my sheet.
Every row has a date cell that has values in this format "2017-02-01 19:33:56+00:00" I would like it to display it in this format (02 January 2017) but I would like to have the original format hidden on the same cell.
Is there a script or something that I can use to display the date in that format?
Just add a custom numberformat from the format menu. This one should do the trick "dd MMM yyyy". The date doesn't change so if you want to go back to the old one you always can. You can use a script to switch between them using range.setNumberFormat
You can use this script to setNumberFormats():
SpreadsheetApp.getActiveRange().setNumberFormat("dd MMM yyyy");
I not familiar with the ss+00:00 not sure what that means. But perhaps you can checked the documentation below. You might be able to use range.getNumberFormat() to get and save that format for future use.
var formatString=SpreadsheetApp.getActiveRange().getNumberFormat();
Number Formats Documentation
This format "2017-02-01 19:33:56+00:00" is not being recognized for Google Sheets, it's because it has an additional value to the final that indicates the timezone of the date "+00:00", i decided to create an small script for getting ride of that timezone value and also add/sub hours and minutes to the date so i can see the correct hour in my timezone.
from datetime import datetime, timedelta
def formatDatetime(o_date, f_hours=0, f_minutes=0):
# ----- Removing the timezone from the data str
date_a = o_date.split('+')
date = date_a[0]
#----------------------------------------------------
# ----- Format/Add/Sub Hours and minutes
date = datetime.strptime(date, '%Y-%m-%d %H:%M:%S')
date = date + timedelta(hours=f_hours)
date = date + timedelta(minutes=f_minutes)
date_f = format(date, '%m/%d/%Y %H:%M:%S')
# -------------------
return date_f
Now google sheet recognizes this value as a date, so i can make a custom formatting for showing a nice date in my worksheet.

Redshift COPY Statement Date load error

I am loading the data using COPY command.
My Dates are in the following format.
D/MM/YYYY eg. 1/12/2016
DD/MM/YYYY eg. 23/12/2016
My target table data type is DATE. I am getting the following error "Invalid Date Format - length must be 10 or more"
As per the AWS Redshift documentation,
The default date format is YYYY-MM-DD. The default time stamp without
time zone (TIMESTAMP) format is YYYY-MM-DD HH:MI:SS.
So, as your date is not in the same format and of different length, you are getting this error. Append the following at the end of your COPY command and it should work.
[[COPY command as you are using right now]] + DATEFORMAT 'DD/MM/YYYY'
Not sure about the single digit case though. You might want to pad the incoming values with a 0 in the beginning to match the format length.

Categories