Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

parsing XSD:dateTime

Posted on 2006-06-16
3
Medium Priority
?
2,287 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
1 Comment
 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
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

783 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