[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Compare dates using ISO 8601 format.

Posted on 2007-07-30
4
Medium Priority
?
506 Views
Last Modified: 2008-01-09
I am being given a registration date in ISO 8601 format and need to get the difference between that date/time and current date and time.

I am being supplied 2007-07-22T15:59:53.4000000-04:00 and need to be able to alert "registered 7 days ago".

Hope this makes sense. Points for urgency.

Thanks,
N
0
Comment
Question by:nickleplated
  • 2
  • 2
4 Comments
 
LVL 12

Accepted Solution

by:
netsmithcentral earned 2000 total points
ID: 19594970
You'll have to parse the date as a string, then pass it into a date object, then do the compare.

<script type="text/javascript">
var dateStr = [your ISO 8601 date goes here];
var dateArr = dateStr.substring(0, 10).split('-');

var theDate = new Date(dateArr[0], dateArr[1], dateArr[2]);
var today = new Date();

var diff = Math.floor((today - theDate) / 86400000);  //Difference is in milliseconds, convert to days

alert("Registered " + diff + " days ago.");
</script>
0
 
LVL 1

Author Comment

by:nickleplated
ID: 19595144
Sweet - thank you kindly.

N
0
 
LVL 1

Author Comment

by:nickleplated
ID: 19595189
Quick question.

When I used "2007-07-25T15:09:32.4800000-04:00", I get -26 days ago... Seems the month is off by one?
0
 
LVL 12

Expert Comment

by:netsmithcentral
ID: 19595391
It appears you are right.  Sorry I didn't realize that before.  Here's a devguru info page on date object months: http://www.devguru.com/technologies/javascript/10612.asp.

And to update the script change this:
var theDate = new Date(dateArr[0], dateArr[1], dateArr[2]);

To this:
var theDate = new Date(dateArr[0], parseInt(dateArr[1])-1, dateArr[2]);
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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…
Suggested Courses

873 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