javascript - 怎么写一个函数,传一个字符串()进去可以实现各种日期形式的增减?并且考虑到当月是28,30,31天
仅有的幸福
仅有的幸福 2017-06-10 09:48:49
[HTML讨论组]

function thc_displayTime(strDate,type)
{

if (strDate == null) {return "";}
if (strDate == "" ) {return "";}
var r = thc_getMatchTimeArr(strDate);
if ( r== null) {
    return strDate;
}

if (r[r.length-1] == " AM" ) {
    var hh = parseInt(r[5], 10);
    if (  (!isNaN(hh)) && hh == 12 ) {
        r[5] = 0;
    }
}
if (r[r.length-1] == " PM" ) {
    var hh = parseInt(r[5], 10);
    if (  (!isNaN(hh)) && hh < 12 ) {
        r[5] = hh + 12;
    }
}
if(type==1) //short date
{
    return String.format("{{0}}\/{{1}}\/{{2}} ", r[1], r[3], r[4]);
}
else if(type==2) //mm/dd/yy hh:mm 
{
    return String.format("{{0}}\/{{1}}\/{{2}} {{3}}:{{4}}", r[1], r[3], r[4], r[5], r[6]);
}
else if(type==3) //mm/dd hh:mm 
{
    return String.format("{{0}}\/{{1}} {{2}}:{{3}}", r[1], r[3], r[5], r[6]);
}
else if(type==4) //mm/dd/yy hh:mm 
{
    return String.format("{{0}}\/{{1}}\/{{2}} {{3}}:{{4}}", r[1], r[3], r[4].substr(2), r[5], r[6]);
}
else if(type==5) //yy-mm-dd
{
    return String.format("{{0}}-{{1}}-{{2}} ", r[4], r[1], r[3]);    
}
else if(type==6) //yy/dd/mm hh:mm 
{
    return String.format("{{2}}\/{{1}}\/{{0}} {{3}}:{{4}}", r[1], r[3], r[4], r[5], r[6]);
}
else if(type==7) //mm/dd/yyyy hh:mm 
{
    return String.format("{{0}}\/{{1}}\/{{2}} {{3}}:{{4}}", r[1].length == 1 ? "0" + r[1]:r[1], r[3], r[4], r[5], r[6]);
}
else if(type==8) //mm/dd/yy
{
    return String.format("{{0}}\/{{1}}\/{{2}}", r[1], r[3], r[4]);
}
else
{
    return String.format("{{0}}\/{{1}}\/{{2}} {{3}}:{{4}}:{{5}}", r[1], r[3], r[4].substr(2), r[5], r[6], r[8]);
}

}

仅有的幸福
仅有的幸福

全部回复(1)
女神的闺蜜爱上我

moment用这个库吧 基本什么情况都能满足

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号