Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why I see 2016 instead of 2015? #136

Closed
CosmaVito opened this issue Oct 25, 2015 · 1 comment
Closed

Why I see 2016 instead of 2015? #136

CosmaVito opened this issue Oct 25, 2015 · 1 comment

Comments

@CosmaVito
Copy link

I want to convert a date as a string into type date.
E.g: Oct 4 = ' 2015-10-04 00:00:00 '

I try this:
import parsedatetime as pdt

c = pdt.Constants()
p = pdt.Calendar(c)
f = '%Y-%m-%d %H:%M:%S'

data_format = p.parseDT(date_time)  //In this case date_time= "Oct 4"
print data_format[0].strftime(f) 

This code works, but strangely i see 2016 instead of 2015.
So I see this: ' 2016-10-04 00:00:00 '

@idpaterson
Copy link
Collaborator

There is an option on Constants that allows you to interpret all dates without a year as relative to the current year. The default behavior is to assume that a date prior to today's date is referring to the following year.

Simply set the YearParseStyle to zero if you need this alternate behavior:

c.YearParseStyle = 0

For example, with the default value c.YearParseStyle = 1, today (Oct 25, 2015) if I parsed Oct 26 I would get Oct 26, 2015. For Oct 24 I would get Oct 24, 2016. January 4 would of course also parse as 2016. With c.YearParseStyle = 0 all of these dates will parse as 2015.

See #99 for more details

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants