Moment.js is a JavaScript date library for parsing, validating, manipulating, and formatting dates. UTC stands for Universal Time Coordinated which is maintained by the Bureau International des Poids et Measures (BIPM).
Moment.js uses the moment() function by default to parse and display in local time. Thus, we need to use moment.utc() function which will implement in UTC mode for all its display methods instead of local time.
Syntax:
moment.utc();
moment.utc(Number);
moment.utc(String);
Parameters: This function will accept a single parameter that denotes the date. It can in the format of a number, string, or date.
Return Value: It returns the date and time in UTC.
Note: This will not work in the normal Node.js program because it requires the moment.js library to be installed.
Moment.js can be installed using the following command:
npm install moment
Example 1: Implementing moment.utc() function
JavaScript
// Acquiring the pluggin
const moment = require("moment");
// Local time
let a = moment();
// UTC time
let b = moment.utc();
console.log("Using Local time:", a);
console.log("Using UTC Mode:", b);
Output:
Using Local time: Moment<2022-12-13T22:33:50+05:30>
Using UTC Mode: Moment<2022-12-13T17:03:50Z>
Example 2: Passing parameters to moment.utc() function
JavaScript
// Acquiring the pluggin
const moment = require("moment");
let a = moment();
let b = moment.utc(1);
let c = moment.utc("2005-06-10");
console.log("Using Local time:", a);
console.log("Using UTC Mode:", b);
console.log("Using UTC Mode:", c);
Output:
Using Local time: Moment<2022-12-13T22:40:09+05:30>
Using UTC Mode: Moment<1970-01-01T00:00:00Z>
Using UTC Mode: Moment<2005-06-10T00:00:00Z>
References: https://momentjs.com/docs/#/parsing/utc/
Similar Reads
Moment.js Parsing String Moment.js is a date library for JavaScript that parses, validates, manipulates, and formats dates. We can use the moment() function passed with a string to parse dates represented as strings. The moment() function is used to create a moment using a string. Syntax: moment(String) Parameters:Â It take
1 min read
Moment.js Parsing Now Moment.js is a date library for JavaScript that parses, validates, manipulates, and formats dates. We can use the moment() function passed to get the current date and time. The moment() function is used to create a moment with the current date and time. Syntax: moment() | moment(undefined) | moment(
1 min read
Moment.js Parsing Date Moment.js is a date library for JavaScript that parses, validates, manipulates, and formats dates. We can use the moment() function passed with an object to parse dates represented as Date object. The moment() function is used to create a moment using a Date object. Syntax: moment( Date ) Parameters
1 min read
Moment.js Parsing Validation Date Validation in Moment.js moment#isValid is used to check whether a date is considered or not using Moment.js. This can be checked using moment#parsingFlags which returns an object in its response. Before proceeding, please install the moment.js library using the following command. Moment.js mome
2 min read
Moment.js Parsing Array Moment.js is a date library for JavaScript that parses, validates, manipulates, and formats dates. We can pass an array to the moment() function to parse a date represented in the form of an array. The moment() function is used to create a moment using an array. Syntax: moment(Number[]) Parameters:
1 min read