I have a number 2802 which is stored as a string in the backend. I want to split this number into 2 parts as 28 and 02 which should be shown as date 28/02? How can I do it with '/' in between them?
Try this : you can use substring
as shown below. substring
used for getting string between start index and end index. In your case, get string from 0 to 1 and then 2 to 3.
var str = "2802"; str = str.substring(0,2) + "/" + str.substring(2,4); alert(str);
Solution with regex
var res = "2802".match(/\\d{2}/g).join('/'); document.write(res);
Are you asking about simple string manipulation?
var str = "1234";
var res = str.substr(0, 2)+"/"+str.substr(2,4);
You can do this:
var str = "2802"; str = str.split('').map(function(el, i){ if(i == 2){ el = '/'+el} return el; }); document.querySelector('pre').innerHTML = str.join('');
<pre></pre>
With regular expression:
var str = "2802"; str = str.replace(/(.{1,2}$)/gi, '/$1'); document.querySelector('pre').innerHTML = str;
<pre></pre>
var str = "2802";
var output = [str.slice(0, 2), str.slice(2)].join('/');
In this context conside 2802, 28 is date and 02 is month
Here 112 , 028 what is date and month ?
A more generic solution could be
var num = 2802;
var output = String(num).match(new RegExp('.{1,2}', 'g')).join("/");
replace 2 with which ever number to split the number after n digits.
var n = 2;
var output = String(num).match(new RegExp('.{1,'+n+'}', 'g'));
var db_date = '112';
var date_str = '';
if(db_date.slice(0,1)==0){
var date_val = db_date.slice(0,2);
var month_val = db_date.slice(2,3);
if(month_val<=9){
month_val = '0'+month_val;
}
}else{
var date_val = db_date.slice(0,1);
date_val = parseInt(date_val);
if(date_val<=9){
date_val = date_val.toString();
date_val = '0'+date_val;
}
var month_val = db_date.slice(1,3);
}
alert(date_val+'/'+month_val);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.