当前位置:首页 > 开发 > 编程语言 > 编程 > 正文

格式化时间的函数

发表于: 2008-11-19   作者:crabdave   来源:转载   浏览:
摘要: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><HTML> <HEAD>  <TITLE> New Document </TITLE>  <META NAME="Generator" C

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
 <HEAD>
  <TITLE> New Document </TITLE>
  <META NAME="Generator" CONTENT="EditPlus">
  <META NAME="Author" CONTENT="">
  <META NAME="Keywords" CONTENT="">
  <META NAME="Description" CONTENT="">
  <script>
 

//格式化时间的函数
 Date.prototype.format = function(formatter)
{
    if(!formatter || formatter == "")
    {
        formatter = "yyyy-MM-dd";
    }
    var year = this.getYear().toString();
    var month = (this.getMonth() + 1).toString();
    var day = this.getDate().toString();
    var yearMarker = formatter.replace(/[^y|Y]/g,'');
    if(yearMarker.length == 2)
    {
        year = year.substring(2,4);
    }   
    var monthMarker = formatter.replace(/[^m|M]/g,'');
    if(monthMarker.length > 1)
    {
        if(month.length == 1)
        {
            month = "0" + month;
        }
    }   
    var dayMarker = formatter.replace(/[^d]/g,'');
    if(dayMarker.length > 1)
    {
        if(day.length == 1)
        {
            day = "0" + day;
        }
    }   
    return formatter.replace(yearMarker,year).replace(monthMarker,month).replace(dayMarker,day);   
}

Date.parseString = function(dateString,formatter)
{
    var today = new Date();
    if(!dateString || dateString == "")
    {
        return today;
    }
    if(!formatter || formatter == "")
    {
        formatter = "yyyy-MM-dd";
    } 
    var yearMarker = formatter.replace(/[^y|Y]/g,'');  
    var monthMarker = formatter.replace(/[^m|M]/g,'');  
    var dayMarker = formatter.replace(/[^d]/g,'');
    var yearPosition = formatter.indexOf(yearMarker);
    var yearLength = yearMarker.length;
    var year =  dateString.substring(yearPosition ,yearPosition + yearLength) * 1;
    if( yearLength == 2)
    {
        if(year < 50 )
        {
            year += 2000;
        }
        else
        {
            year += 1900;
        }
    }
    var monthPosition = formatter.indexOf(monthMarker);
    var month = dateString.substring(monthPosition,monthPosition + monthMarker.length) * 1 - 1;
    var dayPosition = formatter.indexOf(dayMarker);
    var day = dateString.substring( dayPosition,dayPosition + dayMarker.length )* 1;
    return new Date(year,month,day);
}
  </script>
 </HEAD>

 <BODY>
    <script>
 //格式化时间示例
 alert("解析格式化好的日期(MM月dd日yy年):"+Date.parseString(new Date().format("MM月dd日yy年"),"MM月dd日yy年"));
 var today = new Date();
 var todayString = today.format("yyyy-MM-dd");
 alert("格式化今天的日期(yyyy-MM-dd):"+todayString);
 alert("格式化今天的日期(MM月dd日yy年):"+today.format("MM月dd日yy年"));
 var parsedDate = Date.parseString(todayString);
 alert("解析今天的日期:"+parsedDate);
 var strDate=new Date("Thu Oct 9 20:06:07 UTC+0800 2008");//字符串
 var strDateFormat=strDate.format("yyyy-MM-dd");
 alert("格式化字符串日期后:"+strDateFormat);
  </script>
 </BODY>
</HTML>

格式化时间的函数

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号