How to use the parseZone function from moment

Find comprehensive JavaScript moment.parseZone code examples handpicked from public code repositorys.

moment.parseZone is a function in the Moment.js library that parses a date string with a specified time zone and returns a Moment.js object.

452
453
454
455
456
457
458
459
460
461
	str = str.replace("Z","+00:00");//incoming jobs have wrong date format, this attempts to correct it
}
if (tz == "50") //translate between momentjs and ffastrans timezone
	tz = "30"
var to_parse = str.replace(/...$/,":" + tz);
var parsed = moment.parseZone(to_parse)
return parsed.format("YYYY-MM-DD HH:mm:ss");
  }catch(ex){

console.error("Error getDate: " +str);
fork icon3
star icon14
watch icon3

+ 2 other calls in file

How does moment.parseZone work?

moment.parseZone function takes a date string and a time zone string as input. It then uses the Date.parse() method to parse the date string and creates a Date object with the parsed timestamp.

The parseZone function then applies the specified time zone to the created Date object and creates a Moment object. The Moment object represents the given date and time in the specified time zone.

If the time zone is not specified, the parseZone function defaults to the local time zone of the browser or the Node.js environment.

Finally, the parseZone function returns the Moment object.

Ai Example

1
2
3
4
5
6
7
8
const moment = require("moment-timezone");

const dateString = "2022-05-15T10:30:00";
const timeZone = "America/New_York";

const parsedDate = moment.parseZone(dateString + timeZone);

console.log(parsedDate.format()); // output: 2022-05-15T10:30:00-04:00

In this example, moment.parseZone is used to parse the dateString with the timeZone of America/New_York. The resulting Moment object represents the same date and time as the original dateString, but in the America/New_York time zone. The parsedDate.format() method is used to format the resulting Moment object as a string, which prints out the date and time with the offset -04:00, which represents the four hours difference between the America/New_York time zone and Coordinated Universal Time (UTC-4).