Date Manipulation With Moment

How do I format a date as ISO date(mongodb) using moment.js?
https://stackoverflow.com/questions/63052605/how-do-i-format-a-date-as-iso-datemongodb-using-moment-js

selectedDate = moment(new Date(), ‘YYYY-MM-DDT’)
selectedDate.toISOString() : ‘2021-09-15T10:00:46.039Z’

elem.start_time: ‘Wed Sep 15 2021 10:26:07 GMT+0530 (India Standard Time)’

new Date() : Wed Sep 15 2021 15:43:58 GMT+0530 (India Standard Time)
new Date().toISOString(): ‘2021-09-15T10:07:56.402Z’

moment(new Date(elem.start_time),”YYYY-MM-DD”) – Error
moment(elem.start_time,”YYYY-MM-DD”)

———–
https://momentjs.com/docs/#/use-it/

var moment = require(‘moment’); // require
moment().format();

Or in ES6 syntax:

import moment from ‘moment’;
moment().format();

Parse
=====

Instead of modifying the native Date.prototype, Moment.js creates a wrapper for the Date object. To get this wrapper object, simply call moment() with one of the supported input types.

The Moment prototype is exposed through moment.fn. If you want to add your own functions, that is where you would put them.

Please read:

moment(…) is local mode. Ambiguous input (without offset) is assumed to be local time. Unambiguous input (with offset) is adjusted to local time.
moment.utc(…) is utc mode. Ambiguous input is assumed to be UTC. Unambiguous input is adjusted to UTC.
moment.parseZone() keep the input zone passed in. Ambiguous input is assumed to be UTC.
moment.tz(…) with the moment-timezone plugin can parse input in a specific time zone.
Keep in mind that a time zone and a time zone offset are two different things.

Now
===
To get the current date and time, just call moment() with no parameters.

var now = moment();

This is essentially the same as calling moment(new Date()).

String
======
moment(String);

When creating a moment from a string, we first check if the string matches known “ISO 8601” formats, we then check if the string matches the “RFC 2822 Date time “format before dropping to the fall back of new Date(string) if a known format is not found.

var day = moment(“1995-12-25”);

To specify iso-8601 parsing use moment.ISO_8601 constant.

moment(“2010-01-01T05:06:07”, moment.ISO_8601);
moment(“2010-01-01T05:06:07”, [“YYYY”, moment.ISO_8601]);

# By default, moment parses and displays in local time.
If you want to parse or display a moment in UTC, you can use moment.utc() instead of moment().

While in UTC mode, all display methods will display in UTC time instead of local time.
moment().format(); // 2013-02-04T10:35:24-08:00
moment.utc().format(); // 2013-02-04T18:35:24+00:00

# It is important to note that though the displays differ above, they are both the same moment in time.

var a = moment();
var b = moment.utc();
a.format(); // 2013-02-04T10:35:24-08:00
b.format(); // 2013-02-04T18:35:24+00:00
a.valueOf(); // 1360002924000
b.valueOf(); // 1360002924000

Any moment created with moment.utc() will be in UTC mode, and any moment created with moment() will not.

To switch from UTC to local time, you can use moment#utc or moment#local.

//Test Date address
          // console.log(`moment().toISOString() Default UTC: ${moment().toISOString()}`)
          // console.log(`moment().toISOString(true) : ${moment().toISOString(true)}`)
          // console.log(`moment().format() ISO 8601, no fractional seconds : ${moment().format()}`)
          // console.log(`moment().toObject() : ${moment().toObject()}`)
          // console.log(`moment().toString() : ${moment().toString()}`)

          // moment().toISOString() Default UTC : 2021-09-15T11:05:12.077Z
          // moment().toISOString(true) : 2021-09-15T16:35:12.374+05:30
          // moment().format() : 2021-09-15T16:35:12+05:30
          // moment().toObject() : [object Object]
          // moment().toString() : Wed Sep 15 2021 16:35:13 GMT+0530

Monentjs Tutorial

Moment.js tutorial

var moment = require( 'moment' );

var localDate = new Date();
var localMoment = moment();
var utcMoment = moment.utc();
var loc_utc_mom = moment.utc('2022-07-28T05:02')
var utcDate = new Date( utcMoment.format() );

//These are all the same
// console.log( 'localData unix = ' + localDate.valueOf() );
// console.log( 'localMoment unix = ' + localMoment.valueOf() );
// console.log( 'utcMoment unix = ' + utcMoment.valueOf() );

//These formats are different
console.log( 'localDate = ' + localDate );
console.log( 'localMoment string = ' + localMoment.format() );
console.log( 'utcMoment string = ' + utcMoment.format() );
console.log( 'loc_utc_mom string = ' + loc_utc_mom.format() );
console.log( 'utcDate  = ' + utcDate );
console.log( '\n *********-----*****');
//One to show conversion
console.log( '\nlocalDate as UTC format = ' + moment.utc( localDate ).format() );
console.log( 'localDate as UTC unix = ' + moment.utc( localDate ).valueOf() );

Reference

Moment/JavaScript – Generate an array of time slots with a 15min cliff

https://www.section.io/engineering-education/nodejs-date-and-time-objects-with-moment/

https://www.geeksforgeeks.org/node-js-moment-module/

https://jsfiddle.net/okachynskyy/4ehb0L5p/


https://momentjs.com/docs/#/parsing/now/
https://momentjs.com/docs/#/parsing/string-format/
https://www.tutorialsteacher.com/javascript/javascript-date

Convert 12 hour (AM/PM) string to 24 Date object using moment js
get next week start and end using jquery and moment js
Find next instance of a given weekday (ie. Monday) with moment.js
How do I format a date as ISO date(mongodb) using moment.js?
How do I format a date as ISO 8601 in moment.js?
Convert system date to ISO format using momentjs

https://momentjscom.readthedocs.io/en/latest/moment/04-displaying/01-format/

Leave a Reply

Your email address will not be published. Required fields are marked *