Can not compare date using Angular.js/Javascript - javascript

I have an issue. I can not compare todays date with previous date using Angular.js/Javascript. I am explaining my code below.
var today=new Date();
if(today >= new Date($scope.date1.split("-").reverse().join(","))){
alert('Please select todays date or upcoming date');
}
Here i am getting $scope.date1 value like this 2016-10-18 format.Here i could not compare while date is 2016-10-18 .Here i need while selected date is previous date of todays date that alert will display.Please help me.

new Date($scope.date1.split("-").reverse().join(",")) will not create a valid date.
//Pass the string directly
var nDate = new Date('2016-10-18');
var today = new Date();
if (today >= nDate) {
console.log('Please select todays date or upcoming date');
}

You cannot compare dates as you are doing.
When you initialize date object with new Date(), it is set with current time.
So
var today = new Date("2016-10-19"); //----> current date here
var anothertoday = new Date();
shall never be the same
anothertoday > today //-----> true
The above expression evaluates to true because if you see the time of both the dates
today.getHours() //---> 0
anothertoday.getHours() //---> current time shall be displayed
To compare it on the basis of only date you need to set time of anothertoday to 0 by anothertoday.setHours(0,0,0,0)
Now the above expression should evaluate to false
anothertoday > today //---->false
So in your case your code should be similar to this
var today = new Date();
$scope.date1 = "2016-10-18";
$scope.datearr = $scope.date1.split("-");
var yesterday = new Date($scope.datearr[0],$scope.datearr[1] - 1,$scope.datearr[2]);
today.setHours(0,0,0,0); //----> set time to 0 hours
if(today > yesterday)
console.log("Please select todays date or upcoming date");

Related

Checking if date is greater than today

Trying out a Jquery to confirm if date selected is equal to today or greater than.
If i select today, it return it as the day selected is less than today. Selecting previous day works well but selecting today returns less than. Any tip.
var firstRepaymentDate = new Date($('#First_Repayment_Date').val());
var today = new Date();
if (firstRepaymentDate.getTime() < today.getTime()) {
alert('The First Repayment Date Can only Be Today Or Future Date');
return false;
}
Don't forget that new Date() will include the current time as well. You'll need to remove that time component with today.setHours(0,0,0,0) for the comparison to be correct.
Also, setHours() returns the underlying value like getTime() so you can do
var firstRepaymentDate = new Date($('#First_Repayment_Date').val());
var today = new Date();
if (firstRepaymentDate.getTime() < today.setHours(0,0,0,0)) {
alert('The First Repayment Date Can only Be Today Or Future Date');
return false;
}
In response to the comment about adding 20 days:
This is a little more detailed but is fairly easy.
var today = new Date();
var plus20Days = new Date(today.setDate(today.getDate() + 20));
again you can then use setHours() to reset the time component.
new Date() considers time too, not only the date. I think the easiest way to achieve this is to compare years, months and days by using respectively getFullYear() , getMonth() , getDate().
Check all the methods that manipulate js Date here
https://developer.mozilla.org/it/docs/Web/JavaScript/Reference/Global_Objects/Date

How to check if Event Date is today in jquery [duplicate]

I want to check my date is greater than current date
$("#EndDate").val() ="5/13/2014" ->M/d/y
Please find below code
if (Date.parse(new Date()) > Date.parse($("#EndDate").val())) {
//condition satisfied for today date too.
}
so my end date is today date.but still current date greater than end date. why ? how can i check and validate this. i understood some time value is greater than end date. but i want to check only date/month/year not time.
If:
$("#EndDate").val();
returns a string in m/d/y format, you can turn that into a date object using:
function parseDate(s) {
var b = s.split(/\D/);
return new Date(b[2], --b[0], b[1]);
}
To create a comparable date, do what you are already doing:
var today = new Date();
today.setHours(0,0,0,0);
So now you can do:
if (parseDate($("#EndDate").val()) > today) {
// date is greater than today
}
or if you really must:
if (+parseDate($("#EndDate").val()) > new Date().setHours(0,0,0,0)) ...
Please note that when you do:
Date.parse(new Date().setHours(0, 0, 0, 0))
firstly a new date is created from new Date(). Calling setHours() sets the time value, but the return value from the call is the UTC time value of the Date object.
Date.parse expects a string that looks something like a date and time, so if you pass it a number time value something like 1399903200000, the implementation will fall back to some implementation heuristics to either turn it into a Date or NaN.
So please don't do that. Parsing any string with Date.parse is implementation dependent (even the strings specified in ECMA5) and will return different results in different browsers. So please don't do that either.
Try as below:
var end_date = "05/12/2014"
if(new Date() > new Date(end_date))
{
alert('End date should be greater than Start date');
}
fiddle
i found solution my self by modifying code like this
if (Date.parse(new Date().setHours(0, 0, 0, 0)) > Date.parse($("#EndDate").val())) {
//
}
to avoid time comparison. is there any other better way to do compare dates in efficient manner without any plugin.
var startDate = moment.parse("18/02/2013", "DD/MM/YYYY");
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1; //January is 0!
var yyyy = today.getFullYear();
if(dd<10) {
dd='0'+dd
}
if(mm<10) {
mm='0'+mm
}
today = dd+'/'+mm+'/'+yyyy;
if (today.isAfter(startDate)) {
...
}
Your asking for
check my date is greater than current date
and your giving us this ..
if (Date.parse(new Date()) > Date.parse($("#EndDate").val())) {
//condition satisfied for today date too.
}
just have to reverse your condition like that :
if (Date.parse(new Date()) < Date.parse($("#EndDate").val())) {
//condition satisfied for today date too.
}
However, I am late to answer this, but here is my approach to solve this issue:
var TodayDate = new Date();
var endDate= new Date(Date.parse($("#EndDate").val()));
if (endDate> TodayDate) {
// throw error here..
}

How to validate user input is current date and time?

In html i use ext.net datefield
<ext:DateField runat="server" ID = "date" Format="Y-m-d hh:mm:ss" SubmitFormat="Y-m-d H:i:s" MarginSpec="0 0 0 60" FieldLabel="Gate In Date/Time" AllowBlank="false" IndicatorText="*" IndicatorCls="red-text"/>
when it view it shows correct date but the time is always 12:00:00.In javascript
i do it like this
var iframeID = $('iframe').attr('id');
var myDate = lazyMethod_get(iframeID, "formdetail", "date")
var today = new Date().toISOString().slice(0, 10);
alert(myDate);
alert(today);
if (myDate > today) {
alert("Entered date is greater than today's date ");
}
else {
alert("Entered date is less than today's date ");
}}
the validation always alert the date entered less than today date. I just want to validate the date and time if the user insert for example 2016-02-03 the date is wrong and get current date.
Thank you.
Time in microseconds.
if (!Date.now) {
Date.now = function now() {
return new Date().getTime();
};
}
Do your formatting after you do your calculations.
What is your lazyMethod_get() workings. Is it returning a string, or a Date object.
When you do:
new Date().toISOString().slice(0, 10);
toISOString generates a UTC date (i.e. the time zone offset is 00:00) so it may be a different date from the host system.
E.g. for a user in time zone +10:00 when it is 09:30 on 25 May, the above will return a date string for 24 May. Similarly, for systems where the offset is west of Greenwich, dates will be one day later for times in the evening (e.g. 20:30 on 25 May in time zone -04:00 will be 00:30 on 26 May UTC).
First - The Ext.NET DateField only returns a date (MM-dd-yyyy) not a time. The time always defaults to 12:00:00.
I would compare the year, day, and month of each.
javascript:
var isToday = function (field) {
var fieldDate = field.getValue();
var today = new Date()
if ((today.getFullYear() + today.getMonth() + today.getDate())
== (fieldDate.getFullYear() + fieldDate.getMonth() + fieldDate.getDate())) {
return true;
}
else {
return false;
}
}
Markup:
<ext:DateField ID="DateField1" runat="server">
<Listeners>
<Select Handler="alert( isToday(#{DateField1}) )" />
</Listeners>
</ext:DateField>

how to get day format from new Date in Angularjs

i want to get the value of the day format from new Date()(current date) in my angularjs projet. I try this code in my javascript file:
var today = (new Date()).toISOString();
console.log(today.getDay());
when running my code, i get this message error :
TypeError: today.getDay is not a function
however there are many solutions with this syntax.
How can i fix it please. Any help is appreciated
Use getDay on the Date object not on the ISO string:
var today = (new Date()).getDay();
getDay returns a value from 0(Sunday) to 6(Saturday).
If you want current date and day according to your timezone then ->
var today = new Date().getDay() // 0(Sunday) to 6(Saturday).
var currentDate = new Date().getDate()
If you want current date and day according to UTC timezone then ->
var today = new Date().getUTCDay() // 0(Sunday) to 6(Saturday).
var currentDate = new Date().getUTCDate()
You can get date by using below code
let dayno = new Date(this.date.getFullYear(), this.date.getMonth() ,20).getDay();<br>
if date is 20-11-2019 then Day No is :3

validate the form using javascript where i have from and to date picker and it should allow only current date and valid time not the next day

I have a form where have to enter from and to date in date picker , but while saving the data the from date and to date should be for the current date. And if i enter to date as tomorrows date it should not allow to save the date.Kindly let me know as how to do this in javascript. May I know what is the correct way to achieve my objective? And even say from time is 10 AM then to time should be 12 pm like not less than from time . Please advice in achieving this using javascript
var today = new Date();
var lastDate = new Date(today.getFullYear(), today.getMonth() + 1, 0);
$("#datepicker").datepicker({
startDate: today,
endDate: lastDate,
});
var counter=0;
$("#datepicker").change(function(){
var date1 = $("#datepicker").datepicker('getDate');
date1=date1.getDate();
if(today.getDate() != date1)
{
if(counter%2==0)
alert("please select current date");
}
counter++;
});
Check working fiddle http://jsfiddle.net/GX82L/15/
Done as per your expectation ,like it should prompt if other than current date is selected

Categories