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 06-20-2012, 08:43 PM   PM User | #1
jason_kelly
Regular Coder

 
Join Date: Sep 2011
Posts: 140
Thanks: 88
Thanked 0 Times in 0 Posts
jason_kelly is an unknown quantity at this point
String Question

Hello,

I need your help,

I need to be able to extract both DateA and DateB from a string that will always read:

Ex. 1 (format: dd/mm/yyyy)

if (dateformat = dd-mm-yyyy) { reformat to: dd/mm/yyyy }
------------------------
DateA-DateB
20/06/2012-23/06/2012

alert("DateA = 20/06/2012")
alert("DateB = 23/06/2012")


How can I parse these dates with either the and get their value?

Much thanks and appreciation

Jay
jason_kelly is offline   Reply With Quote
Old 06-20-2012, 08:47 PM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,200
Thanks: 59
Thanked 3,996 Times in 3,965 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
Clear as mud.

You mean if somebody gives you
Code:
20-06-2012-23-06-2012
how do you separate out the two dates?

It's trivial if they give you
Code:
20/06/2012-23/06/2012
You just do
Code:
var both = "20/06/2012-23/06/2012";
var temp = both.split("-");
var dateA = temp[0];
var dateB = temp[1];
But if all the delimiters are dashes, it's trickier. Is that what you are asking about?
__________________
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 06-20-2012, 08:54 PM   PM User | #3
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,200
Thanks: 59
Thanked 3,996 Times in 3,965 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
One way: Treat ANY non digit character the same:
Code:
var both = "20/06-2012$23:06=2012";

both = both.replace( /[^\d]/g,"-" ); // convert all non-digits to dash
var temp = both.split("-");

if ( temp.length != 6 ) { alert("kablooey! invald"); }

var dateA = temp.slice(0,3).join("/");
var dateB = temp.slice(3).join("/");
__________________
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
Reply

Bookmarks

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 06:19 PM.


Advertisement
Log in to turn off these ads.