Javascript number split

Can anyboyd help me split this date number in javascript so that when it is displayed on the screen it has slashes between the 4th and 5th number and the 6th and 7th numbers so that it can be understood by voice vxml browser, Number can be any value, so I need it to work for any eight-digit number.

Same:

20100820

2010/08/20

Many thanks

+4
source share
6 answers

If you have a simple line:

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

For the 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, for example, the dd / mm / yyyy format):

 a.replace(/\b(\d{4})(\d{2})(\d{2})\b/g, "$1/$2/$3") 
+7
source

You can use the substring function for this. Assuming you always have the same input format (e.g. yyyymmdd), this can be done as follows:

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

more about subscript function can be found at: http://www.w3schools.com/jsref/jsref_substring.asp

+3
source

Use this javascript:

 var objRegExp = /(\d{4})(\d{2})(\d{2})/; var ourdate = "12334556"; var formateddate = ourdate.replace(objRegExp, "$1/$2/$3"); 

Now in formateddate a string of formatted date will be indicated.

+1
source
 var s = 20100820 + ""; // make the integer a string var t = ""; for(var i=0; i<s.length; i++) { if(i == 4) // we're at the 4th char t += "/"; if(i == 6) // we're at the 6th char t += "/"; t += s.charAt(i); } console.log(t); 
0
source
 alert(20100820..toString().replace(/^(.{4})(.{2})/, "$1/$2/")) 

PS. You should accept some answers (see https://stackoverflow.com/faq for details).

0
source
 var date ='20100317'; var output = date.replace(/(\d{4})(\d{2})(\d{2})/i,"$1/$2/$3"); alert(output); 
0
source

Source: https://habr.com/ru/post/1304354/


All Articles