PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Thu Dec 18, 2014 7:33 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: days from an event
PostPosted: Wed Jul 25, 2012 3:21 pm 
Offline
Forum Contributor

Joined: Fri Sep 05, 2008 3:34 pm
Posts: 236
Location: Victoria, BC
Syntax: [ Download ] [ Hide ]
var oldevent = new date(2012, 7, 2);
var today = new date;
document.write(today-oldevent);


I am expecting days so any problems with this approach?

_________________
Hardcore Games Legendary is the Only Way to Play


Top
 Profile  
 
 Post subject: Re: days from an event
PostPosted: Wed Jul 25, 2012 6:06 pm 
Offline
Jack of Zircons
User avatar

Joined: Thu Nov 09, 2006 9:30 pm
Posts: 4484
Location: California, USA
Take a look at http://www.adp-gmbh.ch/web/js/date/add_days.html

Oops, this one is closer to what you want to do: http://www.javascriptkit.com/javatutors ... ence.shtml

Unlike Microsoft, Linux/Unix datetime variables store the number of milliseconds, not the number of days.


Last edited by califdon on Wed Jul 25, 2012 6:10 pm, edited 1 time in total.
Added another link


Top
 Profile  
 
 Post subject: Re: days from an event
PostPosted: Thu Jul 26, 2012 8:12 pm 
Offline
Forum Contributor

Joined: Fri Sep 05, 2008 3:34 pm
Posts: 236
Location: Victoria, BC
I guess I need to use a more conservative approach

Syntax: [ Download ] [ Hide ]
<script type="text/javascript">

//Set the two dates
var millennium =new Date(2000, 0, 1) //Month is 0-11 in JavaScript
today=new Date()
//Get 1 day in milliseconds
var one_day=1000*60*60*24

//Calculate difference btw the two dates, and convert to days
document.write(Math.ceil((today.getTime()-millennium.getTime())/(one_day))+
" days has gone by since the millennium!")

</script>
 

_________________
Hardcore Games Legendary is the Only Way to Play


Top
 Profile  
 
 Post subject: Re: days from an event
PostPosted: Thu Jul 26, 2012 8:14 pm 
Offline
Forum Contributor

Joined: Fri Sep 05, 2008 3:34 pm
Posts: 236
Location: Victoria, BC
do I need to check to see if that function will work all the time, such as detect the OS in use?

_________________
Hardcore Games Legendary is the Only Way to Play


Top
 Profile  
 
 Post subject: Re: days from an event
PostPosted: Thu Jul 26, 2012 9:04 pm 
Offline
Jack of Zircons
User avatar

Joined: Thu Nov 09, 2006 9:30 pm
Posts: 4484
Location: California, USA
Vegan wrote:
I guess I need to use a more conservative approach

Syntax: [ Download ] [ Hide ]
<script type="text/javascript">

//Set the two dates
var millennium =new Date(2000, 0, 1) //Month is 0-11 in JavaScript
today=new Date()
//Get 1 day in milliseconds
var one_day=1000*60*60*24

//Calculate difference btw the two dates, and convert to days
document.write(Math.ceil((today.getTime()-millennium.getTime())/(one_day))+
" days has gone by since the millennium!")
</script>

Are you sure you want to use Math.ceil? That rounds upward.

I did it this way, which seems to work:
Syntax: [ Download ] [ Hide ]
<script type='text/javascript'>
   var millennium = new Date(2000, 0, 1);
   var today = new Date();
   var difference_ms = today - millennium;
   var ms_in_a_day = 1000 * 60 * 60 * 24;
   var difference_days = Math.round(difference_ms / ms_in_a_day);
   document.write("Today is "+ difference_days + " days after the millennium.");
</script>

You might want to wrap it in a function, as in this example:
http://www.mcfedries.com/javascript/daysbetween.asp

Quote:
do I need to check to see if that function will work all the time, such as detect the OS in use?

No, Javascript should work the same, regardless of what OS the browser is running in. If your algorithm is correct, it will work.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group