Moment UTC to set a date to another based on logic - javascript

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##

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

How to compare dates without including the hour

So there is a column that has the date with the hour and i was trying to create a variable date with the same date, month, year and hour to be able to compare it wiht that date but this didn't work with me so I thought I would do that by creating the same date but when i compare i won't consider the hour but im facing some difficulties.
anyone of the two solutions would be great
I wrote many other codes but none of them worked and that was the last one i wrote
var date = new Date();
var year = date.getYear();
var month = date.getMonth() + 1; if(month.toString().length==1){var month =
'0'+month;}
var day = date.getDate(); if(day.toString().length==1){var day = '0'+day;}
var date = month+'/'+day+'/'+year;
Logger.log(date);
Im using JavaScript in google app script.
Thank you!
From MDN
We have a first step to create an object date.
let today = new Date()
let birthday = new Date('December 17, 1995 03:24:00')
let birthday = new Date('1995-12-17T03:24:00')
let birthday = new Date(1995, 11, 17) // the month is 0-indexed
let birthday = new Date(1995, 11, 17, 3, 24, 0)
let birthday = new Date(628021800000) // passing epoch timestamp
You can create your Date object following the example above that fits you better. I also recommend giving a good look into this page.
For the second step...
From there, you can use Date.now(). As explained here, this will return "A Number representing the milliseconds elapsed since the UNIX epoch."
The third step is...
comparing both numbers. Which one is smaller will be an "earlier date" and vice-versa.
If some dates don't have time, I would consider it as midnight. Using the default Date format, that would be something like this.
yyyy-mm-ddThh:mm:ssZ
Ex:
2022-02-21T09:39:23Z
The Z at the end means UTC+0.
More about this on this link.
So, a date without time would be:
2022-02-21T00:00:00Z

Choosing a specific day using "moment" javascript

Is there a way to choose a specific day using "moment" in javascript?
For example, say the date is June 20, 2020. I want to go back one month and go to the specific date the 15th (May 15th, 2020)
So far I have:
const date = moment();
date.subtract(1, 'month');
Which would give me the May part, but I'm unsure about the 15th. Thanks in advance.
const date = moment();
date.subtract(1, 'month');
date.date(15);
This should do it.
Refer here
You have an answer with moment.js, bit it's not that difficult with POJS either:
// Create a Date
let d = new Date();
// Set to 15th of previous month
d.setMonth(d.getMonth() - 1, 15);
console.log(d.toString());

How to return a Date format when I use setDate()

I assigned the weekEnd as the current end of the week like this
this.weekEnd = new Date(this.currentDate.setDate(end));
Next, what I want is to give a new value to the weekEnd which is the weekEnd + 7 days. I've done it like below but I can't assign the new value to the weekEnd because the right side returns a Number and not date.
this.weekEnd = this.weekEnd.setDate(this.weekEnd.getDate() + 7);
If you guys have any idea how I can do it I would appreciate it. Thanks a lot!
Just use the .setDate() method without the assignment:
this.weekEnd.setDate(this.weekEnd.getDate() + 7);
.setDate() does two things:
It changes the current Date object to the new date.
Returns the number of milliseconds of that new date since 1 Jan 1970.
In your code you assigned this number to the variable that would have held the correct date anyway.
Try this. It will return 15th Jun and 21st June. If you remove +1 then it will start from sunday. To start from monday you have to add 1.
let current = new Date;
let firstday = new Date(current.setDate(current.getDate() - current.getDay()+1));
let lastday = new Date(current.setDate(current.getDate() - current.getDay()+7));

Getting the next date for a specific day of the week

Given a start date of 2014-07-08 (Tuesday) I would want to perform a check to find the closest day of the week.
For example, I need to be able to perform the following calls:
First Monday (should return 2014-07-14)
First Wednesday (should return 2014-07-09)
First Saturday (should return 2014-07-12)
Etc.
I know moment.js lets you do something like
moment("2014-07-08").day(1)
To get the date of Monday this week, however I need to know if the DOW is before/after the current date and apply the offset accordingly; if that makes any sense..
Any thoughts?
Just add 7 if the day of the week you are looking for is or was before the current day of the week:
var date = moment("2014-07-08");
var dow = date.day();
var nextX = date.day(X + dow <= X ? 7 : 0);

JavaScript date objects UK dates

I have the following code
datePicker.change(function(){
dateSet = datePicker.val();
dateMinimum = dateChange();
dateSetD = new Date(dateSet);
dateMinimumD = new Date(dateMinimum);
if(dateSetD<dateMinimumD){
datePicker.val(dateMinimum);
alert('You can not amend down due dates');
}
})
dateSet = "01/07/2010"
dateMinimum = "23/7/2010"
Both are UK format. When the date objects are compared dateSetD should be less than dateMinimumD but it is not. I think it is to do with the facts I am using UK dates dd/mm/yyyy. What would I need to change to get this working?
The JavaScript Date constructor doesn't parse strings in that form (whether in UK or U.S. format). See the spec for details, but you can construct the dates part by part:
new Date(year, month, day);
MomentJS might be useful for dealing with dates flexibly. (This answer previously linked to this lib, but it's not been maintained in a long time.)
This is how I ended up doing it:
var lastRunDateString ='05/04/2012'; \\5th april 2012
var lastRunDate = new Date(lastRunDateString.split('/')[2], lastRunDateString.split('/')[1] - 1, lastRunDateString.split('/')[0]);
Note the month indexing is from 0-11.
var dateString ='23/06/2015';
var splitDate = dateString.split('/');
var month = splitDate[1] - 1; //Javascript months are 0-11
var date = new Date(splitDate[2], month, splitDate[0]);
Split the date into day, month, year parts using dateSet.split('/')
Pass these parts in the right order to the Date constructor.
Yes, there is problem with the date format you are using. If you are not setting a date format the default date that is used is 'mm/dd/yy. So you should set your preferred date formate when you create it as following when you create the date picker:
$(".selector" ).datepicker({ dateFormat: 'dd/mm/yyyy' });
or you can set it later as:
$.datepicker.formatDate('dd/mm/yyyy');
When you try to create a date object:
new Date(year, month, day, hours, minutes, seconds, milliseconds)
Example:
dateSetD = new Date(dateSet.year, dateSet.month, dateSet.day);
Note: JavaScript Date object's month starts with 00, so you need to adjust your dateset accordingly.

Categories

Resources