function checkDate(sender, args) {
var toDate = new Date();
toDate.setMinutes(0);
toDate.setSeconds(0);
toDate.setHours(0);
toDate.setMilliseconds(0);
if (sender._selectedDate > toDate) {
alert("You can not select forthcoming days!");
sender._selectedDate = toDate;
sender._textbox.set_Value(sender._selectedDate.format(sender._format))
}
I used this javascript to validate the date of the textbox. Its should only accept the all the past date and todays date. Future dates are not accepted. It works perfectly for past and future date. But when i select the todays date it do not accept. The logic is correct because selected date should be greater than todays date as i have given. Can any one give a suggestion on this.
You're checking against the start of the day (ie. 0:00). So unless they go on at exactly midnight selectedDate will be > toDate. You want to set the time fields to 23:59:59 to allow any time during the current day.
Related
I'm using a component from AntUI library that renders the date picker.
What I want to do is to display Now and Tomorrow instead of today's/ tomorrow's dates.
Is there a specific momentJS format (I couldn't find any, so probably not) or a way to configure momentJS library that it will automatically display those values instead of those dates?
You could check if the date is between moment().startOf('day'); and moment().endOf('day'); and display Now or Tomorrow depending on the result
moment().isBetween(moment().startOf('day'), moment().endOf('day'));
would be Now (true) and
moment().add(1, 'day').isBetween(moment().startOf('day'), moment().endOf('day'));
and this would be Tomorrow (false)
Try this:
var today = moment().endOf('day')
var tomorrow = moment().add(1, 'day').endOf('day')
var date = new Date();
//Check Today's date
if (date < today)
alert('today')
if (date < tomorrow)
alert('tomorrow')
UPDATE
You can use this as well but it will show current time as well:
moment().calendar(); // Today at 10:37 AM
moment().add(1, 'days').calendar(); // Tomorrow at 10:37 AM
I need one help.I am unable to compare the selected date with today's date using Angular.js or JavaScript.I am explaining my code below.
var today=new Date();
console.log('2 dates',today,$scope.date);
From the above console i am getting the output like below.
2 dates Fri Jun 03 2016 18:29:16 GMT+0530 (India Standard Time) 03-06-2016
if(today > new Date($scope.date.split("-").reverse().join(","))){
alert('Please select future delivery date');
}
Here i need suppose my selected date is 03-06-2016 and today's date is 03-06-2016 the alert prompt should not come.But in my case its not happening like this.This alert message is coming also if selected date is 03-06-2016. Here i need the alert message should come if selected date is more than today's date only.Please help me.
Months are zero-indexed when constructing Dates:
console.log(new Date(2016,06,03));
"2016-07-03T04:00:00.000Z" // you expected June, but got July.
Note also that that is set to midnight: any new Date() run during the same day will be greater than that value.
String manipulation of dates is risky and error-prone, which is why everyone's knee-jerking momentjs at you (it is in fact a very good library and worth using if you're doing much date manipulation).
At the least, you may want to consider using a less ambiguous date format in $scope.date, but if you're stuck with that, in this case, you need to subtract 1 from the month when constructing a Date from it:
// this sample code will only work correctly on June 3 2016 :)
var $scope = {
date: "03-06-2016", // I am assuming DD-MM-YYYY format here
tomorrow: "04-06-2016"
};
var today = new Date();
// test against today:
var dArr = $scope.date.split('-');
var testDate = new Date(dArr[2],dArr[1]-1,dArr[0]);
if (today > testDate) {
console.log("today is greater than testDate.");
/* Note that anytime during today will be greater than the zero
o'clock constructed from $scope.date. If what you really want
is to alert only on the following day, then add 1 to the day
of $scope.date when converting it to a Date(). */
}
// now test against tomorrow:
dArr = $scope.tomorrow.split('-');
testDate = new Date(dArr[2],dArr[1]-1,dArr[0]);
if (today < testDate) {
console.log("today is less than (tomorrow's) testDate.");
}
Don't compare JavaScript dates using native < or > operators; install and use moment.js - it will handle all the difficulties for you:
if(moment(new Date()).isAfter(moment($scope.date, "MM-DD-YYYY"), 'day')) { ...
Convert to timestamp to compare it:
$scope.date = '2016-06-01';
var currentDate = new Date().valueOf();
var date = new Date($scope.date).valueOf();
if (currentDate > date) {
// something
}
It you use multi timezone, please convert both to a timezone.
I have a Date object and I'm trying to add a year to the today's date. I also have to have a way to compare the date that's in the date object (newly made + one year) and today's date. How do I compare today's date with the date in the variable? The point is, I need to have a way to know if today's date is the same or greater to the expiration date, that way I could redirect users to a different location... thanks all!
here's my Date object
var expDate = new Date();
The basic JavaScript Date object has stuff built in to handle all of that natively.
For adding a year to your date simply do this:
var currentDate = new Date();
var futureDate = new Date(currentDate);
futureDate.setFullYear(futureDate.getFullYear() + 1);
That gets the current day as the date, and then creates a new date, based on today's date and sets the yea to the current year plus 1.
As for the comparison, simply use <, <=, >, >=, ==, and !=. JavaScript understands what to do with those operators when Date objects are involved and will compare the two dates appropriately.
On thing that I might suggest doing (since you only care about the actual date, and not the time element of the Date object, is add in this line of code after getting today's date:
currentDate.setHours(0, 0, 0, 0);
That will set the hour, minute, second, and millisecond values to 0, so that only the date is a factor when doing any comparisons.
For everything that you could ever need to know about the JavaScript Date object, check out here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
Edit: Fixed syntax issue.
I am working on a health website.
I am having a field called Last Menstrual Period, its a textbox which has to be filled in by doctor in format of YYYY-MM-DD.
What I want to do is, I have to add 281 days into the LMP date that the doctor will be entering in order to generate the Expected Delivery Date (Child Birth)
So I need followings thing to do:
The date entered should be in format of YYYY-MM-DD
It should be a valid date i.e. taking care of leap years and invalid dates like 2013-02-31 etc.
After generating the Expected Date of delivery (based on LMP that is entered), it should be displayed on screen.
As soon as the date is entered in the LMP textbox, these validations should be performed and the Expected Date Of Delivery is displayed inside a tag below the LMP textbox
How can I do that? Here is what I have tried so far.
// Calculate Expected Date Of Delivery
$('#lmp_date').change(function()
{
var lmp_entered = $this.val();
if(lmp_entered)
{
// $('#edd').html('1');
}
else
{
// $('#edd').html('Please enter last menstrual date to calculate EDD');
Please help me how to implement these validations and display the date using jquery. I am a newbie in jquery so dont know much about it. Any small help will be highly appreciated.
for validation I don t recommend using another library especially for such a simple rule. if the rule is so strict just write your own and use it site wide.
For example block non-numberic entries to textbox and add dash "-" by your code in every 5th and 9th chars.
Then to validate check the length of text and dashed, use split to get year. month and day separately and convert it to date with Date(txtYear,txtMonth,txtDay) if your date's year, month and day are equals to your txtYear, Month and Day then it means it s a valid date. After that use the below to calculate birth date.
just use Date constructor like below
var delDate = new Date(year, month, day + 281)
Just don t forget months start 0 in JS so Jan = 0, Feb = 1 etc.
I have two datepicker fields (telerik, not jquery UI) and also a radio button list containing buttons for week, month, year, etc.
The user can either select a date range using the two datepickers, or alternatively, they can click one of the radio buttons and the fields should populate based on their choice.
So if the user selects week, the end date should be today and the start date should be 7 days ago.
What I have at the moment is this:
$(function () {
$("#dateRange_week").click(function () {
var now = new Date();
var startDate = now;
$("#StartDate").val(startDate);
$("#EndDate").val(now);
});
});
currently the jQuery inserted dates are strings formatted as follows
Tue Oct 02 2012 12:08:01 GMT-0400 (Eastern Daylight Time)
How do I calculate startDate as now - 7 days?
How can I format the dates as mm/dd/yyyy?
EDIT___
The fix:
Using Date.js as per the accepted answer below, the jQuery becomes
$("#dateRange_week").click(function () {
var startDate = (7).days().ago();
var start = startDate.toString("M/d/yyyy");
var endDate = new Date();
var end = endDate.toString("M/d/yyyy");
$("#StartDate").val(start);
$("#EndDate").val(end);
});
It's a pity, but JS doesn't have good datetime manipulation abilities.
Good news - you can use some plugin for that.
For example:
Moment - very good plugin from SO user timrwood
date.js - another good plugin for date and time manipulation
They both (and many others) have functions for adding/subtracting days and date formatting. And many other useful options.
Here is a good start for formatting the jQuery date jQuery date formatting
And this might help for subtracting days
Adding/Subtracting days from a date doesn't change the year/month correctly
Try something like:
DateTime dtNow = DateTime.Now;
DateTime dtBegin = DateTime.MinValue;
DateTime dtEnd = DateTime.MinValue;
dtBegin = dtNow.AddDays(1 - Convert.ToDouble(dtNow.DayOfWeek));
dtEnd = dtNow.AddDays(7 - Convert.ToDouble(dtNow.DayOfWeek));
To formate mm/DD/yyyy use .ToShortDateString()