strptime
How to convert integer into date object python?
I would suggest the following simple approach for conversion: from datetime import datetime, timedelta s = “20120213” # you could also import date instead of datetime and use that. date = datetime(year=int(s[0:4]), month=int(s[4:6]), day=int(s[6:8])) For adding/subtracting an arbitary amount of days (seconds work too btw.), you could do the following: date += timedelta(days=10) date -= … Read more
How to parse milliseconds?
Courtesy of the ?strptime help file (with the example changed to your value): z <- strptime(“2010-01-15 13:55:23.975”, “%Y-%m-%d %H:%M:%OS”) z # prints without fractional seconds op <- options(digits.secs=3) z options(op) #reset options
How to format date string via multiple formats in python
Try each format and see if it works: from datetime import datetime def try_parsing_date(text): for fmt in (‘%Y-%m-%d’, ‘%d.%m.%Y’, ‘%d/%m/%Y’): try: return datetime.strptime(text, fmt) except ValueError: pass raise ValueError(‘no valid date format found’)
Parsing datetime strings containing nanoseconds
You can see from the source that datetime objects don’t support anything more fine than microseconds. As pointed out by Mike Pennington in the comments, this is likely because computer hardware clocks aren’t nearly that precise. Wikipedia says that HPET has frequency “at least 10 MHz,” which means one tick per 100 nanoseconds. If you … Read more
How can I account for period (AM/PM) using strftime?
The Python time.strftime docs say: When used with the strptime() function, the %p directive only affects the output hour field if the %I directive is used to parse the hour. Sure enough, changing your %H to %I makes it work.
Changing date format in R
There are two steps here: Parse the data. Your example is not fully reproducible, is the data in a file, or the variable in a text or factor variable? Let us assume the latter, then if you data.frame is called X, you can do X$newdate <- strptime(as.character(X$date), “%d/%m/%Y”) Now the newdate column should be of … Read more
What are the “standard unambiguous date” formats for string-to-date conversion in R?
This is documented behavior. From ?as.Date: format: A character string. If not specified, it will try ‘”%Y-%m-%d”‘ then ‘”%Y/%m/%d”‘ on the first non-‘NA’ element, and give an error if neither works. as.Date(“01 Jan 2000”) yields an error because the format isn’t one of the two listed above. as.Date(“01/01/2000”) yields an incorrect answer because the date … Read more
A faster strptime?
Is factor 7 lot enough? datetime.datetime.strptime(a, ‘%Y-%m-%d’).date() # 8.87us datetime.date(*map(int, a.split(‘-‘))) # 1.28us EDIT: great idea with explicit slicing: datetime.date(int(a[:4]), int(a[5:7]), int(a[8:10])) # 1.06us that makes factor 8.
Get date from week number
A week number is not enough to generate a date; you need a day of the week as well. Add a default: import datetime d = “2013-W26” r = datetime.datetime.strptime(d + ‘-1’, “%Y-W%W-%w”) print(r) The -1 and -%w pattern tells the parser to pick the Monday in that week. This outputs: 2013-07-01 00:00:00 %W uses … Read more