博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

字符串轉換成日期(Object)

Posted on 2010-12-07 23:52  ☆Keep★Moving☆  阅读(285)  评论(0编辑  收藏  举报

function StringToDate(sDate)
{
                // Parameters :
// sDate : A string with the format "mm/dd/yyyy" or "mm/dd/yyyy hh:mm:ss"
// Returns : a Date object

if (sDate =="")
{
   sDate="01/01/2040";
}

var ArDateTime = sDate.split (" ");
var ArDate = ArDateTime[0];
var splitDate = ArDate.split ("/");
var nDay = ToNumber(splitDate[1]);
var nMonth = ToNumber(splitDate[0]);
var nYear = ToNumber(splitDate[2]);
if (ArDateTime.length == 1)
      return (new Date(nYear, nMonth-1 , nDay))
else
{     var ArTime = ArDateTime[1];
       var splitTime = ArTime.split(":");
       if (splitTime[0]=="00" && splitTime[1]=="00" && splitTime[2]=="00" )
             return (new Date(nYear, nMonth-1 , nDay))
       else
       {
             var nHours     = ToNumber(splitTime[0]);
             var nMinutes   = ToNumber(splitTime[1]);
             var nSeconds = ToNumber(splitTime[2]);
             return (new Date(nYear,nMonth-1,nDay, nHours, nMinutes, nSeconds))
       }
}

}