Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript number split

Can anyboyd help me split up this date number in javascript so that when it is outputted to the screen it has slashes between the 4th and 5th number and the 6th and 7th number, so that it can be understood by a vxml voice browser. The number can be any value so i need it to work for any eight digit number.

Like so:

20100820

2010/08/20

Many thanks

like image 918
Jaron787 Avatar asked Dec 22 '22 04:12

Jaron787


2 Answers

If you have a simple string:

var a = '23452144';
alert(a.substring(0,4) + '/' + a.substring(4,6) + '/' + a.substring(6));

For a number, you can use

var s = a.toString();

For a long string with many such dates, this will replace their formats (you can easily play with it if you want a dd/mm/yyyy format, for example):

a.replace(/\b(\d{4})(\d{2})(\d{2})\b/g, "$1/$2/$3")
like image 183
Kobi Avatar answered Jan 12 '23 22:01

Kobi


You can use the substring-function for that. Assuming you always have the same input-format (eg. yyyymmdd), it can be done this way:

var dateString = "20100820";
var newValue = dateString.substring(0,4) + "/" + dateString.substring(4,6) + "/" + dateString.substring(6,8);

more on the substring function can be found at: http://www.w3schools.com/jsref/jsref_substring.asp

like image 24
Pbirkoff Avatar answered Jan 12 '23 22:01

Pbirkoff