Get number of days prior to the specified date Javascript [duplicate] - javascript

This question already has answers here:
Date difference in Javascript (ignoring time of day)
(15 answers)
How to subtract days from a plain Date?
(36 answers)
Closed 6 years ago.
I'm reading the value of the date input and need to workout if there is 42 days or more between the specified date and today's date. This is what I've tried so far:
var sdate = new Date($("#holiday-editor input[name=StartDate]").val()); //This is returing date in the string format
var priorDate = new Date().setDate(sdate - 42).toString(); // This is returning some abstract int value
var dateNow = new Date().getDate().toString(); // this is returning 5 even though I'd like to get today's date in the string format
if (dateNow > priorDate) {
$("#HolidayBookedLate").show();
}

If you manipulate dates a lot in your app I'd suggest to use moment.js. It's only 15kb but it has lots of useful features to work with dates.
In your case you can use diff function to get amount of days between two dates.
var a = moment([2007, 0, 29]);
var b = moment([2007, 0, 28]);
a.diff(b, 'days') // 1

Related

How can i get first and last day of current week in ISO Date format in javascript? [duplicate]

This question already has answers here:
JavaScript - get the first day of the week from current date
(20 answers)
Closed 4 years ago.
$gte: ISODate("2010-04-29T00:00:00.000Z"),$lt: ISODate("2010-05-01T00:00:00.000Z")
You may get the day of the week by calling .getDay() on the date object. Once you have the day of the week, rest are arithmetic operations.
To convert Date to ISODateFormat you can use toISOString
var dateForCalculation = new Date();
var prevSunday = new Date(dateForCalculation.setDate(dateForCalculation.getDate()-dateForCalculation.getDay())).toISOString();
var nextSunday = new Date(dateForCalculation.setDate(dateForCalculation.getDate()+7)).toISOString();
console.log(prevSunday);
console.log(nextSunday);

Javascript: Get epoch date as from predefined date is using todays date instead [duplicate]

This question already has answers here:
Convert UNIX to readable date in javascript
(3 answers)
Closed 4 years ago.
JSFiddle: https://jsfiddle.net/u8w3v9fd/1/
I am trying to get the day, month and year from a date that is passed form the database in the format: DD/MM/YYYY. However I can't even seem to get the correct date to show.
Here is my code:
var time = "1522843537";
var regDateOriginal = new Date(time);
var regDate = new Date();
regDate.getMonth(regDateOriginal);
regDate.getHours(regDateOriginal);
regDate.getDate(regDateOriginal);
document.write("<p style='color: #fff'>" + regDate.getDate(regDateOriginal) + "</p>");
As you can see, this is returning:
21
Which is todays date. It should be 4
I have googled it and hacked around with various versions for the past 45 mins. I am a junior and would really appreciated a nicely commented piece of code so I can learn instead of just copying and pasting.
Thank you for your help.
From here
var time = 1522843537;
var d = new Date(0); // The 0 there is the key, which sets the date to the epoch
d.setUTCSeconds(time);
console.log(d.getDate());
Of course, you can still do all the other Date functions as needed.

Invalid date when I put a future timestamp [duplicate]

This question already has answers here:
How to convert milliseconds to date and time?
(3 answers)
Closed 5 years ago.
Suppose to have a future timestamp date.
var timestamp="1511858535000" //Future timestamp
var date=new Date(timestamp);
console.log(JSON.stringify(date));// this line give me Invalid Data
Anyone can help me to understand why?
The timestamp you mentioned in the comments works fine, but you have to pass it into the Date constructor as a number, not a string:
var timestamp = "1511858535000";
var date = new Date(Number(timestamp));
console.log(JSON.stringify(date));

Add 30 days to the time now [duplicate]

This question already has answers here:
Add 30 days to date (mm/dd/yy)
(4 answers)
Closed 6 years ago.
I'm using Date.now() to get the current date.
I then need to add 30 days to this.
How can I do this?
Would I just work out how many seconds in 30 days, then add this on?
var d = new Date();
console.log(d);
d.setDate(d.getDate() + 30);
console.log(d);
Date.prototype.getDate returns current date day number.
Date.prototype.setDate set date number of given date to value passed to it.
If value exceeds normal date range of month date extra value will handle needed math and change month (or year if necessary) and shows correct result.
var date = new Date;
date.setDate(date.getDate() + 30);
console.log(new Date, date);

Compare dates in JavaScript [duplicate]

This question already has answers here:
Compare two dates with JavaScript
(44 answers)
Closed 8 years ago.
I want to compare following date in Javascript. Please help me.
$fromdate=2014-12-08
$todate=2014-12-12
I want to compare both the dates with current date. Please tell me how to code in if loop.
You can construct a Date object and then compare them however you want:
// Date months are 0-based
var fromDate = new Date(2014, 11, 8);
var toDate = new Date(); // Today
// Then you can calculate the difference between them
var seconds = (toDate.getTime() - fromDate.getTime())/1000;
var minutes = ~~ (seconds/60);
var hours = ~~ (minutes/60);
var days = ~~ (hours/24);
Then you can use the diff to calculate how many seconds, hours, days etc. there are between them.
A date can be represented numerically as a count of milliseconds from an epoch (01 January, 1970 UTC in javascript).
Create a Date object for today, start and end dates.
Get the numerical representation of these dates by using the method getTime
Now compare today's representation with the start and end representations.
IF the numerical value of today is greater than or equal to the start numerical value, AND
is less than or equal to the end numerical value, THEN
today falls within the supplied range, ELSE
it does not.

Categories

Resources