Created
June 25, 2019 18:52
-
-
Save gabemeola/fe9d698cc5786ec183a5b4f9420a2f93 to your computer and use it in GitHub Desktop.
Converts Java SimpleDataFormat to ISO 8601 format
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
/** | |
* Converts Java SimpleDataFormat to ISO 8601 format | |
* | |
* @example | |
* simpleDateIso('2019-01-24T12:42:17:234-0700') => '2019-01-24T12:42:17.234-0700' | |
* | |
* @param {string} simpleDate - Java SimpleDataFormat string | |
* https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html | |
* @returns {string|null} | |
*/ | |
export function simpleDateToIso(simpleDate: string): string | null { | |
try { | |
const dateStamp = simpleDate.split(':'); | |
if (dateStamp.length !== 4) return null; | |
// Time zone is always the last item | |
const timeZone = dateStamp.pop(); | |
// Join together with correct period delimiter | |
return `${dateStamp.join(':')}.${timeZone}`; | |
} catch (error) { | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment