Skip to content

Instantly share code, notes, and snippets.

@gabemeola
Created June 25, 2019 18:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gabemeola/fe9d698cc5786ec183a5b4f9420a2f93 to your computer and use it in GitHub Desktop.
Save gabemeola/fe9d698cc5786ec183a5b4f9420a2f93 to your computer and use it in GitHub Desktop.
Converts Java SimpleDataFormat to ISO 8601 format
/**
* 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