Moment UTC to set a date to another based on logic

Viewed 19

I would like to make it so that if a date is before the 15th day of the month, I set a date variable to the first of that month, and if it's after or on the 15th I would like to set the date variable to the first of the next month.

How would I achieve this in moment? I'm having a major brain-fart !

Date2 === moment.utc(Date1).date() <15? ##not sure what to put here## 

1 Answers

Would something like this work for you?

const some_date = moment.utc(date1);
const day_of_month = some_date.date();
if(day_of_month < 15){
    // reset date2 back to day 1 of this month
    var date2 = some_date.clone().date(1).hours(0).minutes(0).seconds(0);
}else{
    // bump date2 forward to day 1 of *next* month.
    var date2 =some_date.clone().date(1).hours(0).minutes(0).seconds(0).add(1, 'months');
};

You may also want to consider replacing moment.js with one of the alternatives recommended by the Moment.js Project Status.

Related