node.jstimezonejs-joda

How do I use js-joda timezone operations in node?


I am attempting to use @js-joda/core in this code snippet in a node environment:

LocalDateTime.parse(row.date + "T" + row.time)
    .atZone(ZoneId.of("America/Los_Angeles"))
    .toInstant()
    .toEpochMilli()

I get this error:

ERROR: unsupported ZoneId:America/Los_Angeles 

 {
  "stack": "Error\n    at getRules (/Users/kevin/project/node_modules/@js-joda/core/dist/js-joda.js:4395:15)\n    at ofId (/Users/kevin/project/node_modules/@js-joda/core/dist/js-joda.js:4406:39)\n    at of (/Users/kevin/project/node_modules/@js-joda/core/dist/js-joda.js:10262:27)\n    at <anonymous> (/Users/kevin/project/app/app/scripts/create-tags.ts:119:30)\n    at <anonymous> (/Users/kevin/project/app/app/scripts/create-tags.ts:79:25)\n    at map (native)\n    at <anonymous> (/Users/kevin/project/app/app/scripts/create-tags.ts:79:14)\n    at processTicksAndRejections (native)",
  "message": "unsupported ZoneId:America/Los_Angeles"
} 

I can run this code fine in the browser console at https://js-joda.github.io/js-joda/: working

What do I need to set up to make this work in a node environment?


Solution

  • Js-joda package has been deprecated, you can use @js-joda/core instead

    npm i @js-joda/core @js-joda/timezone
    

    and

    you need to import @js-joda/timezone at the top of the file, this will enable time zone

    import '@js-joda/timezone';
    

    then try this

    LocalDateTime.parse(row.date + "T" + row.time)
        .atZone(ZoneId.of("America/Los_Angeles"))
        .toInstant()
        .toEpochMilli()