Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

js日期格式转换的几种方法探讨 #9

Open
zuobaiquan opened this issue Sep 30, 2017 · 0 comments
Open

js日期格式转换的几种方法探讨 #9

zuobaiquan opened this issue Sep 30, 2017 · 0 comments

Comments

@zuobaiquan
Copy link
Owner

提出问题: 如何将 2017年8月22日 转换成 2017-8-22 / 2017-08-22呢

'2017年8月22日'.replace(/[年月日]/g,'-');

'2017年8月22日'.match(/\d+/g).join('-')

'2017年8月22日'.replace(/[年月]/g,'-').replace('日','');

点评 第一种形式 返回的是

 2017-8-22-
 //这时我们可以通过字符串截取来处理
 '2017年8月22日'.replace(/[年月日]/g,'-').slice(0,-1);

上面日期 如果小于10,补0 ,则需要用如下方法

'2017年8月22日'.match(/\d+/g).map(n => +n < 10 ? '0'+n : n).join('-')

function format( str ) {
    var result = /^(\d+)年(\d+)月(\d+)日$/.exec(str)
    if( result ) {
        var y = result[ 1 ];
        var m = result[ 2 ];
        var d = result[ 3 ];
        m = Number( m ) < 10 ? '0' + m : m;
        d = Number( d ) < 10 ? '0' + d : d;
        return y + '-' + m + '-' + d;
    }
    return null;
}
console.log(format( '2017年8月22日'))

'2017年8月22日'.match(/\d{1,4}/g).join('-').replace(/\d+/g, function(d) {
   return (d.length > 1)? d : ('0' + d);
})
     
'2017年8月22日'.replace(/(\d+)日/, function(_d, d) {
   return (d.length == 2)? ('-' + d) : ('-0' + d);
}).replace(/(\d+)月/, function(_m, m) {
   return (m.length == 2)? ('-' + m) : ('-0' + m);
}).replace(/(\d+)年/, function(_y, y) {
   return y;
})
     
'2017年8月22日'.replace(/(\d+)[年,月,日]/g, function(_d,d) {
   return (d.length > 1) ? d.length == 4 ? d : ('-' + d) : ('-0' + d);
})

'2017年8月22日'.replace(/(\d{4})年(\d{1,2})月(\d{1,2})日/, (a,b,c,d)=>{
   return `${b}-${c>9?c:'0'+c}-${d}`
 })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant