How to compare the date part alone from a date time value - javascript

I have two variables namely
date1 = Mon Nov 25 2013 00:00:00 GMT+0530 (IST)
date2 = Mon Nov 25 2013 14:13:55 GMT+0530 (IST)
When I compare the two dates I get that date2 is greater which I need is correct. But I do not want to check the time part of the two dates I have. How could I get the date part alone from these two dates and compare it?
var today = new Date(); //Mon Nov 25 2013 14:13:55 GMT+0530 (IST)
d = new Date(my_value); //Mon Nov 25 2013 00:00:00 GMT+0530 (IST)
if(d>=today){ //I need to check the date parts alone.
alert(d is greater than or equal to current date);
}

Try clearing the time using Date.setHours:
dateObj.setHours(hoursValue[, minutesValue[, secondsValue[, msValue]]])
Example Code:
var today = new Date();
today.setHours(0, 0, 0, 0);
d = new Date(my_value);
d.setHours(0, 0, 0, 0);
if(d >= today){
alert(d is greater than or equal to current date);
}

The best way would be to modify the accepted answer's if statement as follows
if(d.setHours(0,0,0,0) >= today.setHours(0,0,0,0))
In this way, you can easily check for equality as well because the return type for setHours() is integer.

Try:
var today = new Date(); //Mon Nov 25 2013 14:13:55 GMT+0530 (IST)
var d = new Date(my_value); //Mon Nov 25 2013 00:00:00 GMT+0530 (IST)
var todayDateOnly = new Date(today.getFullYear(),today.getMonth(),today.getDate()); //This will write a Date with time set to 00:00:00 so you kind of have date only
var dDateOnly = new Date(d.getFullYear(),d.getMonth(),d.getDate());
if(dDateOnly>=todayDateOnly){
alert(d is greater than or equal to current date);
}

var StartDate = $("#StartDate").val();
var EndDate = $("#EndDate").val();
if ((( EndDate - StartDate)/ (86400000*7))<0)
{
alert("Start Date Must Be Earlier Than End Date"); $("#StartDate").focus();
error = true;
return false;
}

Related

How to set time with AM PM in Javascript Date Object

Say that I have DateTime in this format Fri Feb 02 2018 00:00:00 GMT+0530 (IST)
And from the time picker plugin getting the time 1:10am or 2:30pm in this format.
I am not sure how to calculate and combine/add them both to produce this result:
Fri Feb 02 2018 01:10:00 GMT+0530 (IST) or Fri Feb 02 2018 14:30:00 GMT+0530 (IST)
I wish if there was something to do as simple as this:
new Date(dateString).setHours(1:10am)
Seems like you need to parse it on your own:
function parseDaytime(time) {
let [hours, minutes] = time.substr(0, time.length -2).split(":").map(Number);
if (time.includes("pm") && hours !== 12) hours += 12;
return 1000/*ms*/ * 60/*s*/ * (hours * 60 + minutes);
}
To add it to a date:
new Date(
+new Date("Fri Feb 02 2018 00:00:00 GMT+0530")
+parseDaytime("1:20pm")
);
Here is a simple function to do what your after.
It basically splits the time using a regex, and then calls setHours & setMins, adding 12 hours if pm is selected.
The example below takes the current datetime, and sets 1:10am & 2:40pm..
function setHours(dt, h) {
var s = /(\d+):(\d+)(.+)/.exec(h);
dt.setHours(s[3] === "pm" ?
12 + parseInt(s[1], 10) :
parseInt(s[1], 10));
dt.setMinutes(parseInt(s[2],10));
}
var d = new Date();
console.log(d);
setHours(d, "1:10am");
console.log(d);
setHours(d, "2:40pm");
console.log(d);
You can parse the time string into hours & minutes, adjust the hours according to am/pm & set it to the date object then:
var dateString = 'Fri Feb 02 2018 00:00:00 GMT+0530 (IST)';
var hoursString = '2:30pm';
var parts = hoursString.replace(/am|pm/, '').split(':')
var hours = parseInt(parts[0]) + (hoursString.indexOf('pm') !== -1 ? 12 : 0);
var minutes = parts[1];
var date = new Date(dateString);
date.setUTCHours(hours, minutes);
console.log(date); // in your local time
console.log(date.toUTCString()); // in UTC (i.e. without timezone offset)
(Note setHours / setUTCHours mutates date object but returns unix timestamp of the updated datetime.)

Javascript date conversion does not fail

I have an input date string like so "30/09/1992", and I found this code to suit my need. PFB the code.
var input1 = "30/09/1992";
var isVaidDate = false;
var actualDate = "";
try{
var pattern = /^(\d{1,2})\/(\d{1,2})\/(\d{4})$/;
var arrayDate = input1.match(pattern);
var actualDate = new Date(arrayDate[3], arrayDate[2] - 1, arrayDate[1]);
var isVaidDate = typeof dt.getMonth === 'function';
}catch(e){var output1 = false;}
print(isVaidDate);
print(actualDate);
The above code works fine but when I set the input as "31/09/1992" or "40/09/1992" I am expecting invalid date to come but I get the below output.
for "31/09/1992":
true
Thu Oct 01 1992 00:00:00 GMT+0530 (India Standard Time)
for "40/09/1992":
true
Thu Oct 10 1992 00:00:00 GMT+0530 (India Standard Time)
How should I get this to fail when i pass these two strings. Thanks. Also what is going on and why it didnt fail, would also be useful :)
This example can help you:
var dateString = 'Mon Jun 24 2013 05:30:00 GMT+0530 (India Standard Time)';
var myDate = new Date(dateString);
var final_date = myDate.getDate()+"-"+(myDate.getMonth()+1)+"-"+myDate.getFullYear();
Here you can verify each variable as day, month and year.

Remove GMT+530(Indian standard Time) without changing current time in javascript

I am displaying current day,month,date,year and time like this
Mon Oct 24 2016 17:09:25 GMT+0530 (India Standard Time)​​​​​​​
but i need to display like this
Mon Oct 24 2016 17:09:25
my code in javascript:
var timestamp = new Date();
editor.insertHtml( 'The current date and time is: ' + timestamp.toString());
How can i do this please can anyone tell me how to do this.
Thank you
If you are open to add a library, you should use moment.js
console.log(moment().format('ddd MMM DD YYYY hh:mm:ss'))
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.15.2/moment.min.js"></script>
If not, a small work around
var d = new Date().toString();
var index = d.lastIndexOf(':') +3
console.log(d.substring(0, index))
Note: moment approach is more preferred
var date = new Date();
var n = d.toLocaleString();
document.getElementById("demo").innerHTML = n;
This is work for me.
var timestamp = new Date();
console.log(
timestamp.toString().split('GMT')
)
// Mon Oct 25 2021 17:56:11 GMT+0530 (India Standard Time)`
the Output will be Mon Oct 25 2021 17:55:02
let today = new Date();
today = today.toString();
today = today.split('G')[0];
console.log(today);

Date issues in JavaScript

The code below mentioned is for comparision of date. Both date1 and mydate have similar values,But if i compare its not entering if loop. Any help appreciated
var date_arr = new Array( "Jan", "Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
var Avl_date = document.getElementById("Available_Date").value;
var V_date1 = Avl_date.split('-');
var date1 = new Date (V_date1[2], date_arr.indexOf(V_date1[1]),V_date1[0]);
var myDate = new Date();
myDate.setHours(0,0,0);
//Thu Dec 04 2014 00:00:00 GMT+0530 (IST) --> date1
//Thu Dec 04 2014 00:00:00 GMT+0530 (IST) --> mydate
if(myDate.getTime() === date1.getTime())
{
//Not entering the loop
}
You're not setting the milliseconds of myDate to 0, so it keeps its original milliseconds. Use:
myDate.setHours(0,0,0,0);
Barmar is correct. setting the milliseconds will solve your problem.

How to set Hours,minutes,seconds to Date which is in GMT

I have Date Object ,I wanted to clear HOUR,MINUTE and SECONDS from My Date.Please help me how to do it in Javascript. Am i doing wrong ?
var date = Date("Fri, 26 Sep 2014 18:30:00 GMT");
date.setHours(0);
date.setMinutes(0);
date.setSeconds(0);
Expected result is
Fri, 26 Sep 2014 00:00:00 GMT
How Do I achieve ?
According to MDN the setHours function actually takes additional optional parameters to set both minutes, seconds and milliseconds. Hence we may simply write
// dateString is for example "Fri, 26 Sep 2014 18:30:00 GMT"
function getFormattedDate(dateString) {
var date = new Date(dateString);
date.setHours(0, 0, 0); // Set hours, minutes and seconds
return date.toString();
}
You can use this:
// Like Fri, 26 Sep 2014 18:30:00 GMT
var today = new Date();
var myToday = new Date(today.getFullYear(), today.getMonth(), today.getDate(), 0, 0, 0);
Recreate the Date object with constructor using the actual date.
To parse the date into JavaScript simply use
var date = new Date("Fri, 26 Sep 2014 18:30:00 GMT”);
And then set Hours, Minutes and seconds to 0 with the following lines
date.setHours(0);
date.setMinutes(0);
date.setSeconds(0);
date.toString() now returns your desired date

Categories