Home  >  Q&A  >  body text

javascript - How to write a function that can increase or decrease various date forms by passing a string () into it? And considering that the month is 28, 30, and 31 days

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]);
}

}

仅有的幸福仅有的幸福2710 days ago704

reply all(1)I'll reply

  • 女神的闺蜜爱上我

    女神的闺蜜爱上我2017-06-10 09:50:49

    Use this library for moment. It can satisfy basically any situation

    reply
    0
  • Cancelreply