Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-25-2013, 10:44 PM   PM User | #1
JasonJingle
New to the CF scene

 
Join Date: Jan 2013
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
JasonJingle is an unknown quantity at this point
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)?
JasonJingle is offline   Reply With Quote
Old 01-25-2013, 10:49 PM   PM User | #2
Logic Ali
Regular Coder

 
Logic Ali's Avatar
 
Join Date: Sep 2010
Location: London
Posts: 975
Thanks: 0
Thanked 203 Times in 198 Posts
Logic Ali will become famous soon enoughLogic Ali will become famous soon enough
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( /\:[^:]*$/, "" );
Logic Ali is offline   Reply With Quote
Old 01-25-2013, 10:50 PM   PM User | #3
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,530
Thanks: 0
Thanked 503 Times in 494 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
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/
felgall is offline   Reply With Quote
Old 01-25-2013, 11:34 PM   PM User | #4
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,461
Thanks: 52
Thanked 457 Times in 455 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
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
xelawho is offline   Reply With Quote
Old 01-26-2013, 12:12 AM   PM User | #5
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,553
Thanks: 9
Thanked 480 Times in 463 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
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 5/13)
STATS (2013/5) HTML5:90.2% MOB:15.2% IE7:0.5% IE8:8.4% IE9:8.5% IE10:8.5%
rnd me is offline   Reply With Quote
Old 01-26-2013, 12:18 AM   PM User | #6
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,553
Thanks: 9
Thanked 480 Times in 463 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
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 5/13)
STATS (2013/5) HTML5:90.2% MOB:15.2% IE7:0.5% IE8:8.4% IE9:8.5% IE10:8.5%
rnd me is offline   Reply With Quote
Old 01-26-2013, 12:42 AM   PM User | #7
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,530
Thanks: 0
Thanked 503 Times in 494 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
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/
felgall is offline   Reply With Quote
Old 01-26-2013, 01:19 AM   PM User | #8
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,556
Thanks: 62
Thanked 4,054 Times in 4,023 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Old 01-26-2013, 08:38 AM   PM User | #9
JasonJingle
New to the CF scene

 
Join Date: Jan 2013
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
JasonJingle is an unknown quantity at this point
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.
JasonJingle is offline   Reply With Quote
Reply

Bookmarks

Tags
javascript

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:06 AM.


Advertisement
Log in to turn off these ads.