JavaScript - Date Difference In Days Function Problem
I've written a js function to find the difference between two dates. the format being used is dd/mm/yyyy hh:mm. The function returns correct value when give one set of values, but go wrong with another set. examples are given below.
set 1 : Time 1 = 24/02/2011 09:30 , time 2 = 24/02/2011 16:00 Output is corret here. It gives 6 Hours & 30 Minutes (after converting the difference) set 2: Time 1 = 24/02/2011 09:30 , time 2 = 25/02/2011 16:00 Here, it gives 31 days, 6 Hours & 30 Minutes. My code is given below. Also the alert of dates display strange values. Don't know if it is timezone issue. I wonder what is going wrong here Code: function compareDateTime(frmtime,totime) { var date1 = new Date(frmtime); var date2 = new Date(totime); var diff = new Date(); alert(date1); alert(date2); diff = (date2.getTime()-date1.getTime()); if (diff<0) { alert("From Time cannot be later than To Time!"); return 0; } else if (diff==0) { alert("From Time cannot be equal with To Time!"); return 0; } else { return diff; } } The returned diff value is broken down as following: Code: if (diff>0) { days = Math.floor(diff / (1000 * 60 * 60 * 24)); diff -= days * (1000 * 60 * 60 * 24); hours = Math.floor(diff / (1000 * 60 * 60)); diff -= hours * (1000 * 60 * 60); mins = Math.floor(diff / (1000 * 60)); alert(days+","+hours+","+mins); return true; } what am i doing wrong here? Similar Tutorialsi've written a js function to find the difference between two dates. the format being used is dd/mm/yyyy hh:mm. The function returns correct value when give one set of values, but go wrong with another set. examples are given below. set 1 : Time 1 = 24/02/2011 09:30 , time 2 = 24/02/2011 16:00 Output is corret here. It gives 6 Hours & 30 Minutes (after converting the difference) set 2: Time 1 = 24/02/2011 09:30 , time 2 = 25/02/2011 16:00 Here, it gives 31 days, 6 Hours & 30 Minutes. My code is given below. Also the alert of dates display strange values. Don't know if it is timezone issue. I wonder what is going wrong here Code: function compareDateTime(frmtime,totime) { var date1 = new Date(frmtime); var date2 = new Date(totime); var diff = new Date(); alert(date1); alert(date2); diff = (date2.getTime()-date1.getTime()); if (diff<0) { alert("From Time cannot be later than To Time!"); return 0; } else if (diff==0) { alert("From Time cannot be equal with To Time!"); return 0; } else { return diff; } } The returned diff value is broken down as following: Code: if (diff>0) { days = Math.floor(diff / (1000 * 60 * 60 * 24)); diff -= days * (1000 * 60 * 60 * 24); hours = Math.floor(diff / (1000 * 60 * 60)); diff -= hours * (1000 * 60 * 60); mins = Math.floor(diff / (1000 * 60)); alert(days+","+hours+","+mins); return true; } Please Help... Not sure if this is possible in javascript: I'm looking for two different dates (bill date and due date) on an invoice that are captured by OCR. If one of them exists, but the other does not, I want the empty field to be 14 days before (or after) the other. For example: if the bill date is 7/27/2010 and the due date was not captured, I want to set the due date as 8/10/2010 (14 days after the bill date). If the due date was captured as 8/10/2010, but the due date is blank, I want to assign the bill date as 7/27/2010 (14 days before the due date). if both dates have values, do nothing. Thanks. Hi, I need to add days to a date in javascript, My requirement is as follows: Date is coming from a textbox. eg:- 26/07/2010 days from this statement var day1=document.getElementById('<%=HiddenDate.ClientID %>').value; an eg:- if the date is 28/01/2012 and days Needed to be added=5 the added date should be 02/02/2012. Can anybody help me? Thanks Jamuna Hi Guys, Is it possible to add a certain number of days (determined by a select box option) to a date in the format of Thursday, 26 January, 2012 ?? Thanks I am trying to figure out how to calculate the date 30 days ago. I also will need to be able to change the interval. Anyone have a piece of code that does this? I'm just trying to add X days to a given date object. However, it's not the right answer (did it in PHP to check, and my PHP version works). dateObj = new Date (month + '/' + day + '/' + year); // Grab the object for a given date datestamp = dateObj.getTime(); // Get millisecond timestamp newDate = new Date (datestamp + (1000 * 60 * 60 * 24 * parseInt (daysToAdd))); // Add "daysToAdd" days to the date alert (newDate); It's not daysToAdd days away... Any help is greatly appreciated. I wrote a version not using the Date () object, but rather an array of the days in each month, and then subtracting each month until "day" was below 31, etc. It's a lot less efficient than this method though :P Hello, I have a textbox with a date formatted 'dd/mm/yyyy'. I want to be able to add days to the date so that the month ticks over if the days exceed the days in the month and the year also if the month value passes 12. Currently I have the below. This adds to the days but will not tick over the month. Any help please. Thanks Code: <html> <head> <script type="text/javascript"> function calDate() { var dateArr = document.getElementById('testinput').value; var myDate = new Date(); var day; var month; var year; day = dateArr.substring(0,2); month = dateArr.substring(3,5); year = dateArr.substring(6,10); if (day.charAt(0) == '0') { day = day.charAt(1); } if (month.charAt(0) == '0') { month = month.charAt(1); } myDate.setDate(day); myDate.setMonth(month); // January = 0 myDate.setFullYear(year); alert((myDate.getDate()+28)+ "/" + (myDate.getMonth()) + "/" + myDate.getFullYear()); } </script> </head> <body> <a href="javascript:void(0);" onclick="calDate();"> click</a><input type="text" id="days" value="0"/> </body> </html> Please help I am a newbie and need some assistant. I'm trying to get a full date. In this script I want the user to be able to enter a date and get a 1000 day result a full (date) mm/dd/yyy. So far this give me the year only. I've tried so many ways. <script language="javascript"> var reply = prompt("Please enter the date you and your love begin dating (mm/dd/yyyy)", " "); var newstring = new String(reply); var arrTemp = new Array(); arrTemp = newstring.split("/"); var year = parseInt(arrTemp[arrTemp.length-1]); year += 3; alert("You begin dating your love in " + parseInt(arrTemp[arrTemp.length-1]) + "\nYou should be married before or in the year of " + year); </script> Hello everyone, I have a date script, but I want it to be adjusted so for example today it's September 17th 2009, but I want it to say September 15th 2009, so it removes automaticly 2 days.. I tried allot of things but I am not a javascript expert, I hope someone can help me out! Code: <script language="JavaScript" type="text/javascript"> <!-- var month = new Array(); month[0]="January"; month[1]="February"; month[2]="March"; month[3]="April"; month[4]="May"; month[5]="June"; month[6]="July"; month[7]="August"; month[8]="September"; month[9]="October"; month[10]="November"; month[11]="December"; var day = new Array(); day[0]="Sunday"; day[1]="Monday"; day[2]="Tuesday"; day[3]="Wednesday"; day[4]="Thursday"; day[5]="Friday"; day[6]="Saturday"; today = new Date(); date = today.getDate(); day = (day[today.getDay()]); month = (month[today.getMonth()]); year = (today.getFullYear()); suffix = (date==1 || date==21 || date==31) ? "st" : "th" && (date==2 || date==22) ? "nd" : "th" && (date==3 || date==23) ? "rd" : "th" function print_date() { document.write(day + "," + " " + date + "<sup>" + suffix + "</sup>" + " " + month + "," + " " + year); } // --> </script> <script> <!-- print_date(); //--> </script> Hi Guys, I am trying to implement a countdown on my website. I want to calculate the difference between the current date and Jan 1st 2012 at 12:01am. I need to have the format in dd:hh:mm:ss so i can push it into this class. I've been reading posts but can't get it to work properly. Can anyone help me convert the dates? Thanks in advance. hi, anyone knows how to get the past 6th month date using time difference? like if the current date is given 18-April-2011 then the past 6th month date is 18-October-2010...how can this be done by using time difference? Thanks, verve Hi All, I have a form that captures member registration details. In the admin section .. I would like to change the Status field of a record/s from NEW to PAYMENT DUE after 14 days from the data of submission. The status change should automatically trigger on the 15th day. So when the admin checks the list page he/she should be able to view the updated status field. Any pointers how to accomplish this? Thanks in advance. Vinny which is the real difference between: <script> alert('hello'); </script> and <script> function hi() {alert('hello');} </script> thanks in advance makerjoe hi, can some one help me how to get the time and date difference? given two time and date with the following format like in textbox A: 2011-05-03 17:35:47.0 and textbox B: 2011-05-03 16:35:47.0 then the output would be: 0 days, 1 hour, 0 minutes, 0 seconds regards, verve Hi, all~..According to ECMAScript, the root of the prototype chain is Object.Prototype. Each object has an internal property [[Prototype]] that could be another object or NULL.... However, it also says that every function has the Function prototype object: Function.Prototype, it confused me, because a function is an object, for a function object, what is its function prototype and object prototype..For example: Code: var x = function (n) {return n+1;}; what is the relationships of x, Object.Prototype and Function.Prototype Thx a lot !!!!!!!!! I want to get a digital time clock updated every second in 12 Hrs format but the following code i wrote is not meeting my objective. What can be wrong with the code below- Code: <html> <head> <style> #v{ background-color:navy; height:auto; width:auto; font-size:22pt; color:white; font-weight:bold; padding:20px; text-align:center; marquee-style:alternate; } </style> </head> <body> <div id="v"></div> <script> function showTime() { var dt=new Date(); var h=dt.getHours(); var m=dt.getMinutes(); var s=dt.getSeconds(); //var chr=h; if (m< 10) { m= "0" + m; } if (s < 10) { s = "0" + s; } if (h > 12) { h=h - 12; add = " p.m."; } else { h=h; add = " a.m."; } if (h == 12) { add = " p.m."; } if (h==0) { h="12"; } time=((hour<=10) ? "0" + h : h) + ":" + m + ":" + s + add; document.getElementById("v").innerHTML=time; setTimeout("showTime()",100); } showTime(); </script> </body> </html> Hi all, Basically I'm having trouble writing a date function to return this data: [CODE] // Get Date d=new Date(); a=d.getUTCFullYear(); b=d.getUTCMonth() + 1; c=d.getUTCDate(); e=d.getUTCHours() - 1; f=d.getUTCMinutes(); [CODE] I need it so I can run setinterval on the date function. I'm quite new to javascript (obviously!) so any help will be much appreciated. Thanks I'm trying to verify todays date against my input date, using a check value function, but for some reason when it comes to a day over 9 like 12/10/2009 or 12/20/2009 looks like is only recognizing the first number 12/1/2009 and 12/2/2009 and getting my alert back as to choose a future date, this is my code, any help would be greatly appreciated: var currentTime = new Date() var month = currentTime.getMonth() + 1 var day = currentTime.getDate() var year = currentTime.getFullYear() function checkValues() { if (owt8.dayr.value <=month+'/'+day+'/'+year ) { alert("Please enter a future date!"); return false; } owt8.submit(); alert("Your request has been submited") window.close() } I am trying to create a function that takes a users input in the form xx/xx/xxxx. Then I need to figure out a way to compare to a new date and give which day of the year it is when using: alert("This is the" +foo+ "day of the year."). Im alittle confused on how to take the users input and compare it somehow to give me the day of the year.
I am working on an HTML project that displays a field with a number in the field by each day. What it does is start at 0 on day one. Each day it adds a 1 to the field. Day 2, the field would say 1, then day 3 would say 2, and so on. I am not sure how to approach this. This is also displayed in an HTML format. I want to to be automatic and change as the date changes. I will also need a way to reset it back to zero if possible.
|