Solved

parsing XSD:dateTime

Posted on 2006-06-16
3
2,186 Views
Last Modified: 2012-06-27
JavaScript doesn't natively understand the XSD:date and XSD:dateTime specification, which are returned by many web services. Googling for a solution didn't yield anything useful, so I set out to write my own.

I'm hardly a guru at JavaScript or regular expressions, but this is what I came up with. In practice it seems to handle both XSD:date and XSD:dateTime:

function parseXSDDateString(dateString) {
      var re = /(-)?(\d{4})-(\d{2})-(\d{2})(T(\d{2}):(\d{2}):(\d{2})(\.\d+)?)?(([\+-])((\d{2}):(\d{2})))?/;
      return eval(dateString.replace(re,'new Date(new Date(\'$3/$4/$2 $6:$7:$8 GMT$11$13$14\').getTime() + (1000 * 0$9))'));
}

My question is, is there a better way? A faster way? A cleaner regex? A library that already exists?

XSD:dateTime format is this:
[-]CCYY-MM-DDThh:mm:ss[Z|(+|-)hh:mm]

it can look like any of these:
2001-10-26T21:32:52
2001-10-26T21:32:52Z
2001-10-26T21:32:52.333-04:00
-2001-10-26T21:32:52

XSD:date format is this:
[-]CCYY-MM-DD[Z|(+|-)hh:mm]

and can look like these:
2001-10-26
2001-10-26Z
2001-10-26-04:00
-2001-10-26

XSD:dateTime specification:
http://books.xmlschemata.org/relaxng/ch19-77049.html

XSD:date specification:
http://books.xmlschemata.org/relaxng/ch19-77041.html
0
Comment
Question by:JCinDE
3 Comments
 
LVL 63

Accepted Solution

by:
Zvonko earned 250 total points
Comment Utility
The - at the beginning is for negative centuries. Do you realy need years before Christ?
Here my version:

<script>
function parseXSDDateString(dateString) {
  var Zp=(dateString.charAt(10)=="T")?19:10;
  var xDate = new Date(dateString.substr(0,Zp).replace(/-/g,'/').replace("T"," "));
  if(dateString.length>Zp){
    xDate.setMinutes(xDate.getMinutes()+xDate.getTimezoneOffset());
    if(dateString.charAt(Zp)!="Z"){
      var tZ = dateString.substr(Zp).split(":");
      tZ=tZ[0]*60+(tZ[1]*1);
      xDate.setMinutes(xDate.getMinutes()+tZ);
    }
  }
  return xDate;
}

alert(parseXSDDateString("2001-10-26T21:32:52"));
alert(parseXSDDateString("2001-10-26T21:32:52Z"));
alert(parseXSDDateString("2001-10-26T21:32:52-04:00"));
alert(parseXSDDateString("2001-10-26"));
alert(parseXSDDateString("2001-10-26Z"));
alert(parseXSDDateString("2001-10-26-04:00"));
</script>

0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
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…

763 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now