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());
Related
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.
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
I'm developing a website where registrations for a particular event will open on a certain date (say, January 1, 2019) and will close on another date (say, January 10, 2019). I'm using JavaScript to redirect users to the relevant pages if they try to access it on before the 1st or after the 10 of January.
My code so far:
var d = new Date();
var startDate = new Date(2019, 0, 1, 8);
var endDate = new Date(2019, 0, 10, 23, 59);
if(endDate-d<0) // Past expiration date
window.location = "register-closed.html";
else if(startDate-d > 0) // Before starting
window.location = "register-unavailable.html";
The main problem as you might have guessed is that this code takes the local date and time from the user; if I set the date on my device as 2nd January, 2019, I'm able to access the actual register page, even though it's May right now.
I feel this would be a common problem for many, but I've been unable to find any solution to this. How do I get the REAL date and time for my country (India) instead of the device time?
TL;DR
How do I get the actual date and time for a country (in my case, INDIA) using JavaScript? If I can't use vanilla JS, is there some other method to do so?
PS: If you have any solutions that can only be bypassed using methods more complicated than changing your device time, I'll readily accept them. This whole website is just for a high school event, so I don't expect any skilled hackers to spend their time on this :)
This code will get the date (as in the 30th), month, and year. This uses the new Date(); variable type. It has several uses, and you can get the output in whatever order using something like new Date(year, month, day, hours, minutes, seconds, milliseconds). That would output something like Wed May 22 2019 10:46:32 GMT-0400 (Eastern Daylight Time).
var todaysDate = new Date();
var date = todaysDate.getDate();
var month = todaysDate.getMonth();
var year = todaysDate.getFullYear();
if(date === 10 || month === 0 || year === 2019){
//January is 0 because counting starts at 0
...
}
https://www.w3schools.com/jsref/jsref_obj_date.asp
you should take a look to this topic as it seems to answer to your problem using only vanilla JS. Hope it helps :)
I am developing a daily calendar and need to traverse to the next and previous day. How would I write this in javascript? Will the following calculate correctly when going to the next / previous month? Like C#'s DateTime.Today.AddDays(1) will?
new Date(year, month, day + 1)
My concern is that if I execute this on March 31st, it will calculate March 32nd...which wouldn't work obviously.
If someone could provide a function to do both that would be great!
Thanks in advance!
var dateString = '30 Apr 2010'; // date string
var actualDate = new Date(dateString); // convert to actual date
var newDate = new Date(actualDate.getFullYear(), actualDate.getMonth(), actualDate.getDate()+1); // create new increased date
You don't need to worry. It won't.
date = new Date(2012, 2, 31 + 1);
console.log(date);
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.