René Winkler
René Winkler

Reputation: 7088

Handle unknown date parts in moment.js

I am using moment.js to parse birthdates:

moment(birthdate, 'DD.MM.YYYY')

If all date parts, i.e. day, month and year are available everything is fine. However there are people who dont have an exact birthdate. If only the year of birth is available the above parsing sets the day and month to default values that is the first January and this is wrong.

Can I create a moment object with unknown (nullable) values for the day and month?

Upvotes: 1

Views: 651

Answers (1)

cdhowie
cdhowie

Reputation: 169291

No. The data stored by a moment object is a JavaScript Date object, which typically stores a Unix timestamp internally (exact number of seconds since 1970-01-01 00:00:00 UTC). This single value cannot represent anything other than an exact moment in time.

You will need to use a different structure to represent this "partial date" concept.

Upvotes: 1

Related Questions