View Full Version : String Question

06-20-2012, 08:43 PM

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 }

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


Old Pedant
06-20-2012, 08:47 PM
Clear as mud.

You mean if somebody gives you


how do you separate out the two dates?

It's trivial if they give you


You just do

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?

Old Pedant
06-20-2012, 08:54 PM
One way: Treat ANY non digit character the same:

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("/");