Solved

parsing XSD:dateTime

Posted on 2006-06-16
3
2,222 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 250 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

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…
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
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…

696 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