Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

LINK TO THIS FORUM!

Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

Feedback

"...I have answered some questions and have gotten answers for my questions. Anywhere you can do this on one page helps tremendously..."

Geography

Where in the world do Tek-Tips members come from?

Working with dates and conditional text color change

newme01 (Programmer)
20 Jun 12 18:30
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>
feherke (Programmer)
21 Jun 12 4:33
Hi

Quote (newme01)

Im a quick learner if i just know the basics.
Then I hope this will help with the next steps :
  • Better use 'mm/dd/yyyy' date format, that will be parsed correctly by the Date constructor.
  • After instantiating the two objects just subtract them ( same result as subtracting their milliseconds since epoch value obtainable with getTime() ) and will get the difference in milliseconds.
  • Divide it to get the number of days. round() it if need whole days.

CODE --> FireBug-console

>>> new Date('06/21/12') // wrong
Date {Fri Jun 21 1912 00:00:00 GMT+0300 (BMT)}
>>> new Date('06/21/2012') // correct
Date {Thu Jun 21 2012 00:00:00 GMT+0300 (EEST)}
>>> new Date('06/21/2012')-new Date('01/01/2012')
14857200000
>>> new Date('06/21/2012').getTime()-new Date('01/01/2012').getTime()
14857200000
>>> (new Date('06/21/2012')-new Date('01/01/2012'))/(1000*60*60*24)
171.95833333333334
>>> Math.round((new Date('06/21/2012')-new Date('01/01/2012'))/(1000*60*60*24))
172

Feherke.
http://feherke.github.com/

newme01 (Programmer)
24 Jun 12 9:44

Quote (>>> new Date('06/21/12') // wrong)

Thanks for reply. thats probably why I can make my code to work.

Quote ((1000*60*60*24))

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?




ChrisHirst (IS/IT--Management)
24 Jun 12 9:52

Quote:

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

newme01 (Programmer)
24 Jun 12 10:01
Please disregard my first question. I got the answer here:

Quote (Quote ((1000*60*60*24)))

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

newme01 (Programmer)
24 Jun 12 10:03
Thanks ChrisHirst for the quick reply.


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

how do you code it?
feherke (Programmer)
24 Jun 12 10:11
Hi

Quote (newme01)

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 --> FireBug-Console

>>> d=new Date('06/12/2012')
Date {Tue Jun 12 2012 00:00:00 GMT+0300 (EEST)}
>>> dn=d.getTime()
1339448400000
>>> tn=15*24*60*60*1000
1296000000
>>> dn+=tn
1340744400000
>>> d.setTime(dn)
1340744400000
>>> d
Date {Wed Jun 27 2012 00:00:00 GMT+0300 (EEST)}

Feherke.
http://feherke.github.com/

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Back To Forum

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close