Read XML date (d/M/y) format & convert to date object

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Gretsch
    New Member
    • Sep 2007
    • 31

    Read XML date (d/M/y) format & convert to date object

    Hi,

    I have some XML data which contains a date in dd/mm/yyyy format (ie UK date format).

    In javascript I need to read this date then calculate the age (in days& decimals) of the XML data.

    I can get today's date in milliseconds:
    Code:
    var current_date = new Date();
    var date1_ms = current_date.getTime();
    but I can't see how to read the XMLdate in UK format, I have the XML date (string) in a var called docdate & I thought this should work:

    Code:
       var date2 = Date.parseString(docdate,"d/M/yyyy");
    var date2_ms = date2.getTime();
    Then I can sutract date1_ms from date2_ms to get the age in milliseconds, but the Date.parseStrin g(docdate,"d/M/yyyy"); doesnt work. and my alternative of:
    var date2=Date/parse(docdate." d/M/yyyy")
    ignores my format command and reads the date as US format.

    Can any one give me the correct line oif code, or a better one:-) or point me to a reference. Everything I;ve found formats the date for OUTPUT rather than INPUT.
    Thanks.
  • pbmods
    Recognized Expert Expert
    • Apr 2007
    • 5821

    #2
    Heya, Gretsch.

    To output a date in a custom format, you'll have to use Date::getYear() , Date::getDay(), etc. and manually build the string piecemeal.

    Comment

    • Gretsch
      New Member
      • Sep 2007
      • 31

      #3
      Thanks pbmods,

      ...but I assume Date::getMonth( ) would get the first 2 digits of my dateString (in the mistaken belief that it was in US date format)
      .. or have I misunderstood your suggestion?

      Comment

      • pbmods
        Recognized Expert Expert
        • Apr 2007
        • 5821

        #4
        Heya, Gretsch.

        Good point.

        To do that, you will have to create an array and reference its index. E.g.,
        [code=javascript]
        var months = ['Jan', 'Feb', 'Mar', ..., 'Dec'];
        alert(months[theDate.getMont h() - 1]);
        [/code]

        Comment

        • mrhoo
          Contributor
          • Jun 2006
          • 428

          #5
          the getMonth and other Date methods don't work on strings, but only on Date objects. You don't need to worry about reading a date, but about the way you create the Date from your string input.

          If you are using a dd/mm/yyyy formatted string you can assemble the Date by splitting the string on non digits and using the returned array elements-
          [code=javascript]
          var dString= '01/08/2007';
          var dArray= dString.split(/\D+/);
          var dDate= new Date(dArray[2],dArray[1]-1,dArray[0]);[/code]
          Last edited by pbmods; Sep 14 '07, 11:47 AM. Reason: Added CODE tags.

          Comment

          • Gretsch
            New Member
            • Sep 2007
            • 31

            #6
            Excellent - thanks very much

            Comment

            • pbmods
              Recognized Expert Expert
              • Apr 2007
              • 5821

              #7
              Heya, Mr. Hoo.

              Please use CODE tags when posting source code:

              [CODE=javas cript]
              JavaScript code goes here.
              [/CODE]

              Comment

              Working...