Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

parsing XSD:dateTime

Posted on 2006-06-16
3
Medium Priority
?
2,274 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
[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
3 Comments
 
LVL 63

Accepted Solution

by:
Zvonko earned 1000 total points
ID: 16924173
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 does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…

722 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