The issue you are getting is due to the use of Date.parse()
behind the scenes when you build your new Date
instance. This is something that's actually not recommended, as is described on the MDN page for Date :
Note: parsing of date strings with the Date constructor (and Date.parse, they are equivalent) is strongly discouraged due to browser differences and inconsistencies. Support for RFC 2822 format strings is by convention only. Support for ISO 8601 formats differs in that date-only strings (e.g. "1970-01-01") are treated as UTC, not local.
If your input format is simple enough (i.e. always number of day followed by month followed by year, with valid values), your best bet is probably to create a function that parses your input string and builds a Date
object using Date.UTC()
or the setUTC*()
functions. And write another function that takes a Date
object and builds a string in your target format using the getUTC*()
methods. That way you are sure your date will not be off, and not bothered by timezones.
If your use case is actually more complicated than that (multiple input and output formats), it is probably better to look into using a library like moment.js.