split date time in javascript

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ghjk
    Contributor
    • Jan 2008
    • 250

    split date time in javascript

    I want to split date and time in java script.ex: Date=2009-08-12 and Time=14:30:00
    Code:
    selectedDateTime = 2009-08-12 14:30:00
    date = selectedDateTime.split(" ") + "<br>";
    But I got the same value (2009-08-12 14:30:00). Could someone help me?
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    Originally posted by ghjk
    But I got the same value (2009-08-12 14:30:00). Could someone help me?
    how do you print out the result?

    EDIT: because you’re appending the <br> string the result array is converted and you end up where you begin. see String.split() @ MDC

    Comment

    • ghjk
      Contributor
      • Jan 2008
      • 250

      #3
      solved.
      Code:
      selectedDateTime = "2009-08-12 14:30:00"
      var splitarray = new Array();
      splitarray= selectedDateTime.split(" ");

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        I don’t think you need line #2 (though it makes it obvious)

        Comment

        • onlymukti4u
          New Member
          • Mar 2007
          • 50

          #5
          Code:
          function datetime()
          {
          var selectedDateTime = "2009-08-12 14:30:00" 
          var splitarray = new Array(); 
          splitarray= selectedDateTime.split(" "); 
          document.write(splitarray[0]+"<br/>"+splitarray[1]);
          }
          Last edited by Dormilich; Aug 14 '09, 09:43 AM. Reason: added [code] tags

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            Originally posted by onlymukti4u
            Code:
            document.write(splitarray[0]+"<br/>"+splitarray[1]);
            document.write( ) is not a very suitable method to make output. it will only work if you use inline script (which is not necessarily advantageous).

            Comment

            Working...