Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New to the CF scene
    Join Date
    Jan 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Throw away milliseconds in 2013-01-25 22:31:55.809231

    Hi there.

    I've got a string client side of the form 2013-01-25 22:31:55.809231

    My question is, how can I manipulate this to throw away the milliseconds (and seconds if possible)?

  • #2
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    Quote Originally Posted by JasonJingle View Post
    Hi there.

    I've got a string client side of the form 2013-01-25 22:31:55.809231

    My question is, how can I manipulate this to throw away the milliseconds (and seconds if possible)?

    timeString = timeString.replace( /\:[^:]*$/, "" );

  • #3
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,456
    Thanks
    0
    Thanked 632 Times in 622 Posts
    First load it into a Date object (so as to make sure that it is an actual valid date/time string) and then extract just the pieces you want.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #4
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,762
    Thanks
    55
    Thanked 517 Times in 514 Posts
    Quote Originally Posted by felgall View Post
    First load it into a Date object (so as to make sure that it is an actual valid date/time string) and then extract just the pieces you want.
    a fabulously unreliable approach... this:
    Code:
    var d=new Date("2013-01-25 22:31:55.809231")
      alert(d.getDate())
    gives NaN in FF and IE and 25 in Chrome... regExing it, as per Ali's suggestion would appear much more sensible

  • #5
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,184
    Thanks
    10
    Thanked 569 Times in 550 Posts
    Quote Originally Posted by JasonJingle View Post
    Hi there.

    I've got a string client side of the form 2013-01-25 22:31:55.809231

    My question is, how can I manipulate this to throw away the milliseconds (and seconds if possible)?

    Code:
     "2013-01-25 22:31:55.809231".split(/:\d+\./)[0]
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/1/19) IE7:0.2, IE8:6.7, IE11:7.4, IE9:3.8, IE10:4.4, FF:18.3, CH:43.6, SF:7.8, MOBILE:27.5

  • #6
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,184
    Thanks
    10
    Thanked 569 Times in 550 Posts
    Quote Originally Posted by xelawho View Post
    a fabulously unreliable approach... this:
    Code:
    var d=new Date("2013-01-25 22:31:55.809231")
      alert(d.getDate())
    gives NaN in FF and IE and 25 in Chrome... regExing it, as per Ali's suggestion would appear much more sensible
    Code:
    new Date( "2013-01-25 22:31:55.809231".replace(" ","T")  ).toLocaleString()
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/1/19) IE7:0.2, IE8:6.7, IE11:7.4, IE9:3.8, IE10:4.4, FF:18.3, CH:43.6, SF:7.8, MOBILE:27.5

  • #7
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,456
    Thanks
    0
    Thanked 632 Times in 622 Posts
    Quote Originally Posted by xelawho View Post
    a fabulously unreliable approach... this:
    Code:
    var d=new Date("2013-01-25 22:31:55.809231")
      alert(d.getDate())
    gives NaN in FF and IE and 25 in Chrome... regExing it, as per Ali's suggestion would appear much more sensible
    I didn't say to convert it to a date object that way - just that it needs to be converted to a date object in order to confirm that it is an actual date.

    Ali's suggestion would not work correctly if the value supplied were "2013-52-76 92:81:85.809231" as it would simply return part of the garbage supplied instead of recognising that it is garbage.

    rnd_me has supplied the correct fix for being able to load it into a date object properly.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #8
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    I think a better question might be: Where did that string come from?

    If it came from server-side code, which seems pretty darned likely to me, then why not fix the problem in the server.

    Or, if you know it came from reliable server-side code, then even the first answer by LogicAli is sufficient. I doubt seriously that he is worried about some user entering a date time string like that willy-nilly. Why validate it via JS when *almost* surely it comes from some computerized process (likely a DB field?) in the first place.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #9
    New to the CF scene
    Join Date
    Jan 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi there. Thank you for contributing

    To give a bit more context, the string is coming from the server side. It was originally a python datetime object which had been shoved into a JSON object and sent clientside - so I'm confident the string will always be of that form. Logic Ali's method (although a little less sophisticated) has won the day. I initially did try converting to a Date object, however couldn't seem to get that to give me sensible answers.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •