gr2m / moment-parseformat

A moment.js plugin to extract the format of a date/time string
http://gr2m.github.io/moment-parseformat
Apache License 2.0
45 stars 30 forks source link

7/12/65 → M/D/YY #13

Closed onecreative closed 10 years ago

onecreative commented 10 years ago

moment.parseFormat('7/12/65') parsed the year to 2065, I expected: 1965

gr2m commented 10 years ago

That can't be fixed really, someone could make the case the exact other way around as well. I'd add a check after the call if the year is in the future. If it is, subtract 100 years. Would that work for you?

onecreative commented 10 years ago

I would default to the most likely. I assume it's in the past for two reasons:

  1. It's user-entered data. There probably aren't as many reasons for a user to write a future date as there are for them to write a past date. So why shouldn't the script go with that likelihood?
  2. When a user writes a year in a 2-digit format, it is probably because it's familiar to them. Familiar dates are likely to be in the past.
gr2m commented 10 years ago

It's the default behavior of moment.js

moment('7/12/68', 'D/M/YY').year()
// 2068
moment('7/12/69', 'D/M/YY').year()
// 1969

I'm sure they had good reasons to put it this way. Finding the right defaults is pretty hard, so I rather stick with the ones from moment, lots more smarter people than I work on it ;)

onecreative commented 10 years ago

For what it's worth, here's my after-call check that subtracts 100 years:

if (value.isAfter(moment()) && ref.match(/(?:\/|-)([0-9]{2})$/)) value = value.subtract('year',100);