Solved

Javascript date/time adding/subtracting

Posted on 2009-05-19
2
1,159 Views
Last Modified: 2012-05-07
I know this has been posted before, but I can't seem to find it.  I am doing some adding and subtracting of dates and need to display them on the screen in an HTML format.  If, for example, I have the date

02/03/09 3:15 a.m. and need to subtract 5 hours from it (the number of hours will vary), how can I do this and make sure the date/time are truly a real date and time in the past?
0
Comment
Question by:saturation
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 41

Expert Comment

by:HonorGod
ID: 24425713
In JavaScript, there is a wonderful thing called a Date object.
You can instantiate it and provide a specific date/time.

For example:

  var when = new Date( '02/03/09 3:15 am' )

This datatype has many methods that can be used for extracting different parts of the date.

For example:



<html>
<body>
 
<script type="text/javascript">
 
var when = new Date( '02/03/09 3:15 am' )
 
function D2( val ) {
  return ( val < 10 ) ? '0' + val : val
}
  
document.write( D2( when.getMonth() + 1 ) + '/' +
                D2( when.getDate() ) + '/' + 
                D2( when.getYear() ) + '<br>'
              )
 
document.write( when + '<br>' )
 
var before = when
before.setHours( before.getHours() - 5 )
 
document.write( D2( before.getMonth() + 1 ) + '/' +
                D2( before.getDate() ) + '/' + 
                D2( before.getYear() ) + '<br>'
              )
              
document.write( before + '<br>' )
 
</script>
 
</body>
</html>

Open in new window

1
 
LVL 40

Accepted Solution

by:
Richard Quadling earned 500 total points
ID: 24425760
Javascript dates are held as whole days with the decimal part being in milliseconds.1 day = 86 400 000 millisecondsSo, 5 hours = 5 * 60 * 60 * 1000
<html>
<head>
<title>Demo date</title>
<script type="text/javascript">
var o_Date = new Date();
 
alert(o_Date);
 
o_Date.setTime(o_Date - 5 * 60 * 60 * 1000);
 
alert(o_Date);
</script>
</head>
</html>

Open in new window

0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
window close link 7 35
Html date format 9 20
DataTable column sorting incorrectly 2 22
Scroll 5 news at a time using vticker 2 11
This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question