How do I convert custom datetime value ex. "15:22:03 13/11/2019"
to "13/11/2019 15:22:03" to add value as datetime in models?
Or how do i change default django datetime format?
thank you
You can use this to change the default:
date = models.DateField(blank=False, default=datetime.now().strftime(("%d.%m.%Y %H:%M:%S")))
You can get the % strings at the bottom of this page to make it fit the format you asked for in the column, i can update it for you if you can't get it to work, i'm pretty good with Django.
https://docs.python.org/2/library/datetime.html
You can convert your datetime string to datetime object and insert the date into your database.
from datetime import datetime
date_str = "15:22:03 13/11/2019"
temp_date = datetime.strptime(date_str, "%H:%M:%S %d/%m/%Y").date()
ModelClassName.objects.create(name='ABC', date_time=temp_date)
installed dateutil.parser
in view added
from dateutil.parser import parse as date_parse
and in csv import, in a needed row just added date_parse
reading_date=date_parse(READING_DATE),
Related
I get time data from API response like '2020-02-25T20:53:06.706401+07:00'. Now I want to convert to %Y-%m-%d %H:%M:%s format. But I do not know exactly standard format of that time data.
Help me find the time format!
In your case you can use datetime.fromisoformat:
from datetime import datetime
datetime_object = datetime.fromisoformat("2020-02-25T20:53:06.706401+07:00")
print(datetime_object.strftime("%Y-%m-%d %H:%M:%s"))
Prints
2020-02-25 20:53:1582656786
Other options:
Use the third party dateutil library
Use datetime.strptime which parses the string according to format
You can convert to a datetime object and then optionally recreate the string in a new format as follows:
from datetime import datetime
d = "2020-02-25T20:53:06.706401+07:00"
dt = datetime.strptime(d, "%Y-%m-%dT%H:%M:%S.%f%z")
# Note the capital S
new = dt.strftime("%Y-%m-%d %H:%M:%S")
However the new value here has lost the timezone offset information. I assume that's OK for you. I also used %S instead of %s since I assume that's really what you want. The lowercase %s wouldn't really make sense, and is also not truly supported by Python.
I am currently having an issue converting an incoming datetime string to a datetime object using Python's built in strptime() method. Here is what I currently have:
def fixDateTimeField(datetimeString):
# Convert from 2012-08-07T00:00:00Z to 2012-08-07T00:00:00.000Z
datetime_object = datetime.strptime(datetimeString, "%y-%m-%dT%H:%M:%SZ")
return datetime_object
Like the comment says, I am trying to convert the string "2012-08-07T00:00:00Z" to a datetime object that looks like 2012-08-07T00:00:00.000Z but I am getting an error in my console that says: "ValueError: time data '2012-08-07T00:00:00Z' does not match format '%y-%m-%dT%H:%M:%SZ'". The format seems correct to me and i'm not seeing the issue.
Thanks in advance!
%y is for two-digit years. You have a four-digit year.
Try using %Y instead.
>>> from datetime import datetime
>>> datetimeString = "2012-08-07T00:00:00Z"
>>> print(datetime.strptime(datetimeString, "%Y-%m-%dT%H:%M:%SZ"))
2012-08-07 00:00:00
A nice way to parse your iso-8601 datetime string "2012-08-07T00:00:00Z" to a datetime object is using dateutil.
import dateutil.parser
yourdate = dateutil.parser.parse(datestring)
With strptime:
datetime.datetime.strptime( "2007-03-04T21:08:12", "%Y-%m-%dT%H:%M:%S")
Works. As an other answer said, the Y must be capitalized for strptime to work with 4 digit years.
Is there a way to convert UTC time to a formatted string containing the date?
I currently have a list full UTC time but I need them to be formatted as %Y-%m-%d%H:%M:%S.%f but as a string.
you can use the datetime library. I have used this in the past and it works
from datetime import datetime, timezone
datetime.now(timezone.utc).strftime("%Y%m%d")
This should work:
import datetime
datetime.datetime.strptime("1998-04-18 16:48:36.0","%Y-%m-%d %H:%M:%S.%f").strftime("%Y-%m-%d%H:%M:%S.%f")
'1998-04-1816:48:36.000000'
I'm trying to convert a parameter of type string to a date time. I'm using the dateUtil library
from dateutil import parser
myDate_string="2001/9/1 12:00:03"
dt = parser.parse(myDate_string,dayfirst=True)
print dt
every time i run this i get
2001-09-01 12:00:03
regardless of whether i have dayfirst set as true or Year first set as false. Ideally I just want to have a date in the format DD-MM-YYYY HH:MM:SS. I don't want anything fancy. I am willing to use the datetime library but this doesn't seem to work at all for me. Can anyone give simple expamples of how to convert strings to date time with an explicit format, I'm a noob, so the most basic examples are all i require. I'm using Python 2.7
The problem you're having is that any arguments you pass to parser.parse only affect how the string is parsed, not how the subsequent object is printed.
parser.parse returns a datetime object - when you print it it will just use datetime's default __str__ method. If you replace your last line with
print dt.strftime("%d-%m-%Y %H:%M:%S")
it will work as you expect.
The standard lib (built-in) datetime lib can do this easily.
from datetime import datetime
my_date_string = "2001/9/1 12:00:03"
d = datetime.strptime(my_date_string, "%Y/%m/%d %H:%M:%S")
print d.strftime("%d-%m-%Y %H:%M:%S")
I'm working on Google app engine with Python.
I have stored a datetime object in database and fetched it and applied timezone, and I saved it using template. The result is "2011-03-15 16:54:24.398503+09:00", but what I want is: "2011-03-16 01:54:24" (timezone applied string without millisecond - note that day is changed).
How can I do that?
Once you have the datetime object, use strftime, like so;
from datetime import datetime
datetime.now().strftime('%Y-%m-%d %H:%M:%S')
http://docs.python.org/release/2.6.6/library/datetime.html#strftime-and-strptime-behavior
using strftime is encouraged, but make sure that your localtime is correct. This should be a setting in your app/site assuming you're using django, or you may have to do this manually