Solved

parsing XSD:dateTime

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to change pointer icon to hand icon on mouseover 2 19
Randomize in Owl Carousel v1.3.2 6 20
Change javascript css 1 20
Jquery get each value 2 16
Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

896 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

15 Experts available now in Live!

Get 1:1 Help Now