Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Working with dates and conditional text color change

Status
Not open for further replies.

newme01

Programmer
Jun 19, 2012
7
PH
Hi all,

I have two dates with format [mm/dd/yy]
and I want to get the number of days difference.

and how can I make the text of "X" days to become red if the result is negative

Im a quick learner if i just know the basics. hope you can guide me to a good start on my coding.


here where im at now:

<html>
<head>
<title></title>
</head>
<body>

<script language="javascript">
function subtractdate(form)
{
var a = form.datetextbox1.value;
var b = form.datetextbox2.value;
var datedifference = parseFloat(a) - parseFloat(b) + " days difference"; // <--i think this is not correct
document.getElementById('result').innerHTML = datedifference;
}

</script>

<form>
ie. [mm/dd/yy] - [mm/dd/yy] = X/-X days difference<br><br>

<input type=text name="datetextbox1" onchange="subtractdate(this.form);">-
<input type=text name="datetextbox2" onchange="subtractdate(this.form);">
<div id="result"></div>
</form>


</body>
</html>
 
Hi

newme01 said:
Im a quick learner if i just know the basics.
Then I hope this will help with the next steps :
[ul]
[li]Better [highlight #fcc]use 'mm/dd/yyyy' date format[/highlight], that will be parsed correctly by the [tt]Date[/tt] constructor.[/li]
[li]After instantiating the two objects just [highlight #cfc]subtract them[/highlight] ( same result as subtracting their milliseconds since epoch value obtainable with [tt][highlight #ccf]getTime()[/highlight][/tt] ) and will get the difference in milliseconds.[/li]
[li][highlight #fcf]Divide it[/highlight] to get the number of days. [tt][highlight #cff]round()[/highlight][/tt] it if need whole days.[/li]
[/ul]
Code:
[blue]>>> new Date('06/21/12') // wrong[/blue]
[green][b]Date {Fri Jun 21 1912 00:00:00 GMT+0300 (BMT)}[/b][/green]
[blue]>>> [highlight #fcc]new Date('06/21/2012')[/highlight] // correct[/blue]
[green][b]Date {Thu Jun 21 2012 00:00:00 GMT+0300 (EEST)}[/b][/green]
[blue]>>> [highlight #cfc]new Date('06/21/2012')-new Date('01/01/2012')[/highlight][/blue]
[navy]14857200000[/navy]
[blue]>>> new Date('06/21/2012')[highlight #ccf].getTime()[/highlight]-new Date('01/01/2012')[highlight #ccf].getTime()[/highlight][/blue]
[navy]14857200000[/navy]
[blue]>>> (new Date('06/21/2012')-new Date('01/01/2012'))[highlight #fcf]/(1000*60*60*24)[/highlight][/blue]
[navy]171.95833333333334[/navy]
[blue]>>> [highlight #cff]Math.round([/highlight](new Date('06/21/2012')-new Date('01/01/2012'))/(1000*60*60*24)[highlight #cff])[/highlight][/blue]
[navy]172[/navy]

Feherke.
[link feherke.github.com/][/url]
 
Thanks for reply. thats probably why I can make my code to work.

What are these represent (yr*month*days*hrs)?

-----

What if we do a reverse. like X day(s) + '06/12/2012'

how do you code it?




 
What are these represent (yr*month*days*hrs)?

javascript time intervals are in milliseconds (1/1000 second)

so
(60 seconds per min) * (60 minutes per hour) * (24 hours per day) * 1000 = milliseconds in a day.


Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum
 
Please disregard my first question. I got the answer here:

= 1000ms/sec, 60 sec/min, 60 min/hr, 24 hr/day

 
Thanks ChrisHirst for the quick reply.


What if we do a reverse. like X day(s) + '06/12/2012'

how do you code it?
 
Hi

newme01 said:
What if we do a reverse. like X day(s) + '06/12/2012'

how do you code it?
Instantiate a date.
Get its numeric equivalent.
Calculate the time interval as numeric value. ( Used 15 days in the example. )
Do the math with the two numeric values.
Update the date object.
Code:
[blue]>>> d=new Date('06/12/2012')[/blue]
[green][b]Date {Tue Jun 12 2012 00:00:00 GMT+0300 (EEST)}[/b][/green]
[blue]>>> dn=d.getTime()[/blue]
[navy]1339448400000[/navy]
[blue]>>> tn=15*24*60*60*1000[/blue]
[navy]1296000000[/navy]
[blue]>>> dn+=tn[/blue]
[navy]1340744400000[/navy]
[blue]>>> d.setTime(dn)[/blue]
[navy]1340744400000[/navy]
[blue]>>> d[/blue]
[green][b]Date {Wed Jun 27 2012 00:00:00 GMT+0300 (EEST)}[/b][/green]

Feherke.
[link feherke.github.com/][/url]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top