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.
Related
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.
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);
I am trying UTCString to above format. I can able to convert, problem is after conversion it shows a day before.
var newDate = this.getCellDate(target);
console.log(newDate); --> Dec 05 2014 00:00:00 GMT+0800 (Malay Peninsula Standard Time)
cstDate = newDate.toISOString();
console.log(cstDate); -- > 2014-12-04 --- > **Expected --> 2014-12-05**
Use Date.UTC() method
var now = new Date(), // my date Thu Dec 04 2014 13:02:15 GMT+0300 (RTZ 2 (зима))
year = now.getFullYear(),
month = now.getMonth(),
day = now.getDay(),
hours = now.getHours(),
minutes = now.getMinutes(),
utcDate;
utcDate = new Date(Date.UTC(year, month, day, hours, minutes)); // Thu Dec 04 2014 16:02:00 GMT+0300 (RTZ 2 (зима))
Ext.Msg.alert('UTC Date', Ext.Date.format(utcDate, 'Y-m-d'));
Look at this "Thu Dec 04 2014 16:02:00" - i got utc time(+3 hours)
Fiddle example
Yeah i got the solution. I should not toISOString. instead i need to use toLocaleDateString
custdate = newDate.toLocaleDateString();
dueDate= custdate.split("/").reverse().join("-");
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;
}
I am trying to add two dates:
date start Fri Apr 26 2013 16:08:03 GMT+0100 (Paris, Madrid)
+
date periode Fri Apr 26 2013 00:10:00 GMT+0100 (Paris, Madrid)
I used this code:
var periode=$("#dure").val();
var start = $("#start").val()
var end =$("#end").val();
var dateStart= new Date(start);
console.log('start');
console.log(dateStart);
var date=dateStart.format('yyyy-mm-dd');
per=date+' '+periode;
var datePeriode= new Date(per);
console.log('datePeriode');
console.log(datePeriode);
var dateEnd= dateStart.getTime()+datePeriode.getTime();
console.log('dateEnd');
console.log(dateEnd);
In my JavaScript console, I get:
dateDebut
Fri Apr 26 2013 16:33:11 GMT+0100 (Paris, Madrid)
datePeriode
Fri Apr 26 2013 00:15:00 GMT+0100 (Paris, Madrid)
dateEnd
2733922091000
How can I fix that? Am I missing something?
If you want to add a time period to a date, you basically have to convert both of them into milliseconds.
var date = new Date();
var dateMillis = date.getTime();
//JavaScript doesn't have a "time period" object, so I'm assuming you get it as a string
var timePeriod = "00:15:00"; //I assume this is 15 minutes, so the format is HH:MM:SS
var parts = timePeriod.split(/:/);
var timePeriodMillis = (parseInt(parts[0], 10) * 60 * 60 * 1000) +
(parseInt(parts[1], 10) * 60 * 1000) +
(parseInt(parts[2], 10) * 1000);
var newDate = new Date();
newDate.setTime(dateMillis + timePeriodMillis);
console.log(date); //eg: Fri Apr 26 2013 08:52:50 GMT-0700 (MST)
console.log(newDate); //eg: Fri Apr 26 2013 09:07:50 GMT-0700 (MST)
Convert datePeriod to milliseconds instead of making it into a date object for your addition.
You need to convert the sum to a date. getTime() is in milliseconds since 1-1-1970. So you want to do.
var ending = new Date();
ending.setTime(dateEnd);
console.log(ending);
setTime will set the date properly for you.
https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date/setTime