Created
April 17, 2023 11:13
-
-
Save louicoder/9ea52512c2b51c076fec498c524161d1 to your computer and use it in GitHub Desktop.
Validate and also work with iso dates
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ============================================== | |
// Function validates whether passed string is valid iso format | |
function isIsoDate(str) { | |
if (!/\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}Z/.test(str)) return false; | |
const d = new Date(str); | |
return d instanceof Date && !isNaN(d) && d.toISOString()===str; // valid date | |
} | |
console.log(isIsoDate('2011-10-05T14:48:00.000Z')) | |
console.log(isIsoDate('2018-11-10T11:22:33+00:00')); | |
console.log(isIsoDate('2011-10-05T14:99:00.000Z')); // invalid time part | |
// ============================================== | |
// Function converts iso string date to convert iso date to match local time with offset | |
function returnDateWithOffset(dateTime) { | |
// var startTimeISOString = "2013-03-10T02:00:00Z"; | |
// check dateTime is valid iSO date time string | |
// isIsoDate() | |
var newDate = new Date(dateTime); | |
return new Date(newDate.getTime() + (newDate.getTimezoneOffset() * 60000)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment