date.js 747 B

12345678910111213141516171819202122
  1. /* eslint-disable import/prefer-default-export */
  2. /**
  3. * Get normalized Date object for the ISO formatted date strings.
  4. * Natively, the date object parsed from a ISO 8601 string will be offsetted by the timezone difference, which may result in returning a wrong date.
  5. * See: Github issue #3338.
  6. *
  7. * @param {String} dateString String representing the date.
  8. * @returns {Date} The proper Date object.
  9. */
  10. export function getNormalizedDate(dateString) {
  11. var nativeDate = new Date(dateString);
  12. // NaN if dateString is not in ISO format
  13. if (!isNaN(new Date(dateString + "T00:00").getDate())) {
  14. // Compensate timezone offset
  15. return new Date(nativeDate.getTime() + nativeDate.getTimezoneOffset() * 60000);
  16. }
  17. return nativeDate;
  18. }