JavaScript字串類型轉換成日期類型方法

來源:互聯網
上載者:User

如果字串中有空格或是其他分割符分割,可以用split方法先拆分,返回一個數組,再把這

個數組裡的年月日作為參數傳到new Date()裡產生日期類型;
如過沒有特殊分隔字元,可以用substr(x,y)方法來取子字串來產生日期,substr(x,y

)裡,x表示取字串的開始位置,y表示子串的長度!
如果字串以“/”分割可以用Date.parse(datestr)來轉換

<mce:script language="JavaScript"><!--
 
function StringToDate(DateStr)  
   {        
      var converted = Date.parse('2009/01/05');
      alert(converted);
      alert(DateStr.substr(0,4)+"/"+DateStr.substr(5,2)+"/"+DateStr.substr(8,2));
      var myDate = new Date(converted); 
      alert(myDate);
      alert(myDate.getFullYear()+"/"+ (myDate.getMonth()+1) +"/"+myDate.getDate

());
      if (isNaN(DateStr))  
      {   
          //var delimCahar = DateStr.indexOf('/')!=-1?'/':'-';  
          DateStr = "2008-08-08";
          var arys= DateStr.split('-');  
          var d = new Date(arys[0], arys[1], arys[2]);
          alert(d); 
      }  
      //alert(myDate);
      return myDate;  
   }
// --></mce:script>


 

利用正則處理

new Date(a + "/" + b + "/" + c).getMonth() == b-1

a、b、c只要都是 能轉換為數位變數就可以(甚至可以是null ,布爾值),就能用new

Date(y,m,d)建立一個日期格式,
而按照你的要求,new Date(a,b,c).getMonth() == b-1為TRUE的時候就滿足,否則則不滿足

function IsDate(dateValue) {
    var regex = new RegExp("^(?:(?:([0-9]{4}(-|/)(?:(?:0?[1,3-9]|1[0-2])(-|/)

(?:29|30)|((?:0?[13578]|1[02])(-|/)31)))|([0-9]{4}(-|/)(?:0?[1-9]|1[0-2])(-

|/)(?:0?[1-9]|1d|2[0-8]))|(((?:(dd(?:0[48]|[2468][048]|[13579][26]))|(?:0

[48]00|[2468][048]00|[13579][26]00))(-|/)0?2(-|/)29))))$");
    if (!regex.test(dateValue))
        return false;
    else
        return true;
}
傳過來判斷


執行個體

function checkDate(text) {
    if (!text) return false;
    text = text.replace(/[/-]0?/g, "/");
    if (!text.match(/^d{4}/d{1,2}/d{1,2}$/)) return false;
    var d = new Date(text);
    return [d.getFullYear(), d.getMonth() + 1, d.getDate()].join("/") == text;
}

document.open();
var datas = ["", "2010-01-01", "2010/02/30", "2010/02", "2010/22/30",

"2012/2/29", "2012/02/29"];
for(var i = 0; i < datas.length; i++) {
    document.write(datas[i], "=", checkDate(datas[i]), "<br/>");
}
document.close();


輸出
Assembly code
=false
2010-01-01=true
2010/02/30=false
2010/02=false
2010/22/30=false
2012/2/29=true
2012/02/29=true


常用方法

<script language="網頁特效">
/////////////////////////////////e///////////////////////
// 取得當前日期,格式yyyy-mm-dd
////////////////////////////////////////////////////////
function GetCurrentDate()
{
    var Year=0;
    var Month=0;
    var Day=0;
    var CurrentDate = new Date();

    return ChangeDateToString(CurrentDate);
}


/////////////////////////////////e///////////////////////
// 取得當前日期,格式yyyy-mm-dd hh:mm
////////////////////////////////////////////////////////
function GetCurrentTime()
{
    var Year=0;
    var Month=0;
    var Day=0;
    var CurrentDate = new Date();

    return ChangeTimeToString(CurrentDate);
}

////////////////////////////////////////////////////////
// 將日期類型轉換成字串型格式yyyy-MM-dd
////////////////////////////////////////////////////////
function ChangeDateToString(DateIn)
{
    var Year=0;
    var Month=0;
    var Day=0;

    var CurrentDate="";

    //初始化時間
    Year      = DateIn.getYear();
    Month     = DateIn.getMonth()+1;
    Day       = DateIn.getDate();


    CurrentDate = Year + "-";
    if (Month >= 10 )
    {
        CurrentDate = CurrentDate + Month + "-";
    }
    else
    {
        CurrentDate = CurrentDate + "0" + Month + "-";
    }
    if (Day >= 10 )
    {
        CurrentDate = CurrentDate + Day ;
    }
    else
    {
        CurrentDate = CurrentDate + "0" + Day ;
    }
 

    return CurrentDate;
}

///////////////////////////////////////////////////////
// 將日期類型轉換成字串型格式yyyy-MM-dd hh:mm
////////////////////////////////////////////////////////
function ChangeTimeToString(DateIn)
{
    var Year=0;
    var Month=0;
    var Day=0;
    var Hour = 0;
    var Minute = 0;
    var CurrentDate="";

    //初始化時間
    Year      = DateIn.getYear();
    Month     = DateIn.getMonth()+1;
    Day       = DateIn.getDate();
    Hour      = DateIn.getHours();
    Minute    = DateIn.getMinutes();
 

    CurrentDate = Year + "-";
    if (Month >= 10 )
    {
        CurrentDate = CurrentDate + Month + "-";
    }
    else
    {
        CurrentDate = CurrentDate + "0" + Month + "-";
    }
    if (Day >= 10 )
    {
        CurrentDate = CurrentDate + Day ;
    }
    else
    {
        CurrentDate = CurrentDate + "0" + Day ;
    }
  
     if(Hour >=10)
    {
        CurrentDate = CurrentDate + " " + Hour ;
    }
    else
    {
        CurrentDate = CurrentDate + " 0" + Hour ;
    }
    if(Minute >=10)
    {
        CurrentDate = CurrentDate + ":" + Minute ;
    }
    else
    {
        CurrentDate = CurrentDate + ":0" + Minute ;
    }    
    return CurrentDate;
}
</script>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.