Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create Date in Moment.js

I am not seeing it in the documentation but I am wondering if I can do something similar like in C#

DateTime d = new DateTime(2019,01,01);

I would like to do that with moment

var d = moment(2019,01,01);

but that does not seem to work.

I can see I could do something like

var d = moment('2019-01-01');

but I want to do it separately so I can do something like

Datetime d = new DateTime(now.Year + 1, 01, 01);
like image 609
chobo2 Avatar asked Jul 23 '26 06:07

chobo2


2 Answers

You can use following constructor:

var d = moment({ year: 2019, month: 1, day: 1});

Or:

var d = moment([2019, 1, 1]);
like image 164
Aleksey Avatar answered Jul 24 '26 20:07

Aleksey


You can just use a Template Literal to inject your variables into the moment argument string:

let date = moment(`${moment().year()}-01-01`).add(1, 'years');
like image 21
aviya.developer Avatar answered Jul 24 '26 18:07

aviya.developer