I am working to filter data. Filter will be Last Day, Last Week, Last Month.
My code is :
For Last Week :
dateUntil = Date.now();
dateSince = date.setDate(date.getDate()-7)
Last Month :
dateUntil = Date.now();
dateSince = date.setDate(date.getDate()-30)
In Last Day filter I am using :
dateUntil = Date.now();
dateSince = date.setDate(date.getDate()-1);
In last dates it shows, data from this time to yesterday this time. But I want to set Last dates 12.00 am to 11.59pm.
How can I do this ?
You can use below it worked for me :
var d = new Date();
d.setDate(d.getDate()-1);
alert(d);
Hope below code will help you
var datenow = new Date().toDateString();
var dateUntil = new Date(datenow);
var dateSince = new Date(datenow);
dateSince.setDate(dateUntil.getDate()-1);
Related
I am trying to create a new Date using 2 dates I have. Basically my new date
Output:
Date 1 = (presentdate +1 month , endDate+1 day, presentdate+1 year);
//Example 1:
var presentDate = "11/12/2018";
var endDate = "2/8/2018";
var Dat1 = "12/9/2018"; //new date
//Example 2 :
var presentDate = "11/12/2018";
var endDate = "5/25/2018";
var Dat1 = "12/26/2018"; //new date
//Example 3 :
var presentDate = "1/5/2018";
var endDate = "5/30/2018";
var Date1 = "2/31/2018"; // invalid date
//should've been 2/28/2018 since that is the last day of the month
//Example 4:
var presentDate = "3/5/2018";
var endDate = "10/30/2018";
var Date1 = "4/31/2018"; //Invalid date. Should've been 4/30/2019 since it's last day of the month
My code:
var mPresent = moment(presentDt);
var mEnd = moment(eDt);
var Date1 = moment({
year: mPresent.year(), // get presentDt's year
month: mPresent.add(1, 'month').month(), // get presentDt's month
date: mStart.date() // get startdt day of the month
});
console.log(Date1);
It doesn't work in all cases because not every month have 30,31 days and then there is the leap year. I need to create a date that is valid because in some of these cases it returns invalid date.
Any help is appreciated. Thank you!
I'm not entirely sure what's going on with your code, but I chopped out most of it and basically did a 1-liner to calculate the # of months difference - seems to work.
document.querySelector("#ok").addEventListener("click", function () {
var paymentDate = document.getElementById("presentDate").value;
var etDate = document.getElementById("endDate").value;
var RemainingPayments = moment(etDate).diff(moment(paymentDate), 'months');
console.log(RemainingPayments);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>
<label>Present date</label><input id="presentDate" name="presentDate" type="date">
<label>End Date</label> <input id="endDate" name="endDate" type="date">
<button id='ok'>OK</button>
I'm looking for the fastest method to build a timestamp which represents the current hour starting from the current instant (in general, starting from a timestamp)
Currently I'm doing the following:
var d = new Date();
var year = d.getUTCFullYear();
var month = d.getUTCMonth();
var day = d.getUTCDate();
var hour = d.getUTCHours();
d = new Date(year, month, day, hour);
console.log(d);
console.log(d.getTime());
Is it possible to avoid the second invocation of Date?
If I understand you correctly you want the timestamp of the beginning of the current hour. Then you could simply set the minutes and seconds to 0 in your first Date Object:
var d = new Date();
d.setMinutes(0,0);
console.log(d);
console.log(d.getTime());
You could make it a oneliner, since setMinutes() already returns a timestamp:
var timestamp = new Date().setMinutes(0,0);
Not sure why you're doing that twice, you really don't need to.
var d = new Date();
console.log(d.getHours());
// or
console.log(d.getTime() );
I want to get next day date and next month date using jquery
I tried like following :
//FOR NEXT DAY
var dateString = 'Dec 17, 2013'; // date string
var actualDate = new Date(dateString); // convert to actual date
var newDate = new Date(actualDate.getFullYear(), actualDate.getMonth(), actualDate.getDate()+1);
//FOR NEXT MONTH
var dateString = 'Dec 17, 2013'; // date string
var actualDate = new Date(dateString); // convert to actual date
var newDate = new Date(actualDate.getFullYear(), actualDate.getMonth(), actualDate.getDate()+30);
but which is not good practice :(
please guide how can i get next day date and next month date using jquery function or any good method?
In order to avoid dealing with the number of days in a month, the leap years and so on, use the Moment.js library (http://momentjs.com)
var d = new Date("2012/02/05");
var m = moment(d);
m.add('days', 1);
m.add('months', 1);
d = m.toDate();
Consider also that adding one month will not add the same amount of time depending on the current month.
I want to check two dates in java script. date format is YYYY-MM-dd.
var date1 = 2011-9-2;
var date1 = 2011-17-06;
Can anybody say how can I write condition?
If you mean that you want to compare them and your variables are strings, just use == for comparison.
var date1 = '1990-26-01';
var date2 = '2000-01-05';
if (date1 == date2) {
alert('date1 = date2')
}
else {
alert('something went wrong');
}
There are four ways of instantiating dates
var d = new Date();
var d = new Date(milliseconds);
var d = new Date(dateString);
var d = new Date(year, month, day, hours, minutes, seconds, milliseconds);
Here is the link to complete tutorial and function of creating, comparing dates http://www.w3schools.com/js/js_obj_date.asp
If you want to compare dates , have a look at the JS date object https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date , in particular the getTime() method .
Assuming the format is YYYY-MM-dd (your second date value breaks this rule) and that they are strings...
var date1 = '2011-9-2';
var date2 = '2011-06-17';
var fields = date1.split("-");
var d1 = new Date (fields[0], fields[1]-1, fields[2]);
var fields = date2.split("-");
var d2 = new Date (fields[0], fields[1]-1, fields[2]);
I have created 3 select boxes containing days, months and year. What I really would like is to check after the user has selected a date, if the date is over a year from the current date a message is displayed or so.
Im a little stumped on what to do. Any gidance would be great.
Thanks
var ddlYear = document.getElementById('ddlYear');
var ddlMonth = document.getElementById('ddlMonth');
var ddlDay = document.getElementById('ddlDay');
var y = ddlYear[ddlYear.selectedIndex];
var m = ddlMonth[ddlMonth.selectedIndex];
var d = ddlDay[ddlDay.selectedIndex];
// past
var dt = new Date((y+1), (m-1), d);
var moreThanOnYearAgo = dt < new Date();
// future
var dt2 = new Date((y-1), (m-1), d);
var moreThanOnYearAhead = dt2 > new Date();
The y+1 is because if we're adding one year, and are still less than new Date() (today), then it's more than one year ago.
The m-1 is because months in the Date constructor are an enum, which means January is 0.
Don't reinvent the wheel one more time. Use a library that does validation.
There are 31556926000 milliseconds in a year. Just convert that date to a timestamp and subrtact the current date from it. If the result is greater than 31556926000 from it, is over a year away.
var userDate = new Date("11/29/2010");
var now = new Date();
var year_ms = 31556926000;
if ( userDate.getTime() - now.getTime() >= year_ms ) {
// A year away
} else {
// less than a year away
}