(jsonDate: string)
| 6 | * @returns - An empty string when `jsonDate` is falsey, otherwise a date string in local format |
| 7 | */ |
| 8 | export default function utcToLocal(jsonDate: string) { |
| 9 | if (!jsonDate) { |
| 10 | return ''; |
| 11 | } |
| 12 | |
| 13 | // required format of `'yyyy-MM-ddThh:mm' followed by optional ':ss' or ':ss.SSS' |
| 14 | // https://html.spec.whatwg.org/multipage/input.html#local-date-and-time-state-(type%3Ddatetime-local) |
| 15 | // > should be a _valid local date and time string_ (not GMT) |
| 16 | |
| 17 | // Note - date constructor passed local ISO-8601 does not correctly |
| 18 | // change time to UTC in node pre-8 |
| 19 | const date = new Date(jsonDate); |
| 20 | |
| 21 | const yyyy = pad(date.getFullYear(), 4); |
| 22 | const MM = pad(date.getMonth() + 1, 2); |
| 23 | const dd = pad(date.getDate(), 2); |
| 24 | const hh = pad(date.getHours(), 2); |
| 25 | const mm = pad(date.getMinutes(), 2); |
| 26 | const ss = pad(date.getSeconds(), 2); |
| 27 | const SSS = pad(date.getMilliseconds(), 3); |
| 28 | |
| 29 | return `${yyyy}-${MM}-${dd}T${hh}:${mm}:${ss}.${SSS}`; |
| 30 | } |
no test coverage detected
searching dependent graphs…