How to use the parseZone function from moment-timezone

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

moment-timezone.parseZone is a function in the Moment Timezone library that parses a string representation of a date and time with a timezone offset and creates a Moment object in the specified timezone.

37
38
39
40
41
42
43
44
45
46

const isNumber = args => args.reduce((prev, next) => prev && typeof next === 'number', true);

const parseTime = (str) => {
  try {
    const t = moment.parseZone(str, time_ISO_8601);
    if (t.isValid()) {
      return t;
    }
    throw new Error('Invalid ISO_8601 format time. This is usually caused by an inappropriate format. Please check the input format.');
fork icon47
star icon20
watch icon5

+ 7 other calls in file

34
35
36
37
38
39
40
41
42
const timeNow = moment().tz(timezone);
// Our date might be user input
let testDateInput = Date.parse(date);
let dateMoment;
if (isNaN(testDateInput) === false) {
    dateMoment = moment.parseZone(date);
} else {
    dateMoment = timeNow;
}
fork icon0
star icon0
watch icon1

+ 5 other calls in file

How does moment-timezone.parseZone work?

moment-timezone.parseZone is a function provided by the Moment Timezone library that parses a date string with a timezone identifier and returns a Moment object with the timezone information applied to it.

Ai Example

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

const dateString = "2023-04-08T10:00:00-07:00";
const parsedDate = moment.parseZone(dateString);

console.log(parsedDate); // output: 2023-04-08T10:00:00-07:00

In this example, we use moment.parseZone to parse a date string with a specific timezone offset. The resulting parsedDate object is a Moment object representing the parsed date in the original timezone.