javascript 操作日期 测试(注意浏览器兼容问题)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>javas

cript 操作日期 测试</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<META HTTP-EQUIV="REFRESH" CONTENT="11; URL=http://www.dusystem.com/index.htm"> 2006-03-12 designer Geovin Du-->
<metaname="description"content="涂聚文,缔友计算机信息技术有限公司,经营信息流,物流,人力资源流,资本流的系统解决方案的开发与设计和服务,geoVIstudio.Geovin Du. Systems Solution to Fund flow,and Informationflow,and Material flow,and Control flow,and Human Resource.Appliedsoftware development,design and service。.">
<meta name="description" content="Study,Communicate。 ">
<meta name="Robots" content="all index follow ">
<meta name="Author" content="涂聚文" />
<link rel="shortcut icon" href="http://www.dusystem.com/favicon.ico" type="image/x-icon" />
<link rel="icon" href="http://www.dusystem.com/favicon.ico" type="image/ico" />
<link rel="Bookmark" href="http://www.dusystem.com/favicon.ico">
  <script language="JavaScript">
//此代碼,在IE6上不兼容,FireFox可以。
  function DateAdd(strInterval, NumDay, dtDate) {

  var dtTmp = new Date(dtDate);

  if (isNaN(dtTmp)) dtTmp = new Date();

  switch (strInterval) {

  case "s":

  dtTmp = new Date(Date.parse(dtTmp) + (1000 * parseInt(NumDay)));

  break;

  case "n":

  dtTmp = new Date(Date.parse(dtTmp) + (60000 * parseInt(NumDay)));

  break;

  case "h":

  dtTmp = new Date(Date.parse(dtTmp) + (3600000 * parseInt(NumDay)));

  break;

  case "d":

  dtTmp = new Date(Date.parse(dtTmp) + (86400000 * parseInt(NumDay)));

  break;

  case "w":

  dtTmp = new Date(Date.parse(dtTmp) + ((86400000 * 7) * parseInt(NumDay)));

  break;

  case "m":

  dtTmp = new Date(dtTmp.getFullYear(),(dtTmp.getMonth())+parseInt(NumDay), dtTmp.getDate(), dtTmp.getHours(),dtTmp.getMinutes(), dtTmp.getSeconds());

  break;

  case "y":

  //alert(dtTmp.getFullYear());

  dtTmp = new Date(dtTmp.getFullYear()+parseInt(NumDay),dtTmp.getMonth(), dtTmp.getDate(), dtTmp.getHours(),dtTmp.getMinutes(), dtTmp.getSeconds());

  //alert(dtTmp);

  break;

  }

  var mStr=new String(dtTmp.getMonth()+1);

  var dStr=new String(dtTmp.getDate());

  if (mStr.length==1){

  mStr="0"+mStr;

  }

  if (dStr.length==1){

  dStr="0"+dStr;

  }
   //這個日期年份,一定要用getFullYear(),這樣兼容所有瀏覽器。寫軟件的人員,一定要自己測試一下。否則,日期顯示無效 塗聚文注
  return dtTmp.getFullYear()+"-"+mStr+"-"+dStr;

  }

  </script>

  <script language="JavaScript">

  function calculate(type)

  {

  var entry = document.getElementById(type).value;//自己判断是否合法

  var uValue=document.form.u.value;

  uValue=uValue.replace("-","/").replace("-","/");

  //alert(uValue);


//document.form.age1.value=age;

  document.form.twice.value = DateAdd(type,entry,uValue);

  }
function strToDate(str)
{
  var val=Date.parse(str);
  var newDate=new Date(val);
  return newDate;
}
function strToDate1(str)
{
  var arys= new Array();
  arys=str.split('-');
  var newDate=new Date(arys[0],arys[1],arys[2]);
  return newDate;
}
 function agedem()
 {
 
 var age,d,f,g,s,k,t,h;
d=new Date();
age=d.getFullYear();
s=new Date();
s=s.getUTCDay();
document.form.age3.value=s;
k=new Date();
k=k.getDay();
document.form.age4.value=k;
g=new Date();
f=g.getUTCFullYear();
document.form.age2.value=f;
t=document.form.u.value;
//用以替换FireFox浏览器不兼容的日期格式  否則,日期顯示無效 涂聚文
t=t.replace("-","/").replace("-","/");
  //t=str.split('-');
 // t=Date.parse(t);
h=new Date(t)
 // h=new Date(t[0],t[1],t[2]);
 
  h=h.getFullYear();
document.form.age5.value=h;
//计算年份差
document.form.age6.value=f-h
//alert(f);
}  

var nextYear = new Date(); // 初始化日期对象
  
nextyear.setYear(nextYear.getYear() + 1); // 增加1年
</script>
<script type="text/javascript">

<!--

var m_names = new Array("January", "February", "March",
"April", "May", "June", "July", "August", "September",
"October", "November", "December");

var d = new Date();
var curr_date = d.getDate();
var curr_month = d.getMonth();
var curr_year = d.getFullYear();
document.write(curr_date + "-" + m_names[curr_month]
+ "-" + curr_year);

/* The last two lines above have
to placed on a single line */

//-->
</script>
<html>
<head>
    <title>Javascript Convert String to Date</title>
</head>
<body>

<script type="text/javascript">

var dateString = "03-20-2008";  //因为前面作了条件考虑,可以兼容
 // var dateString = "03/20/2008";  mm/dd/yyyy [IE, FF]
// You can also try the following Date string formats
// var dateString = "2008/03/20";  // yyyy/mm/dd [IE, FF]
// var dateString = "03-20-2008";  // mm-dd-yyyy [IE only]
// var dateString = "March 20, 2008";  // mmmm dd, yyyy [IE, FF]
// var dateString = "Mar 20, 2008";  // mmm dd, yyyy [IE, FF]

// Initalize the Date object by passing the date string variable
var myDate = new Date(dateString);

// get Date part from the date string
document.write("Date : " + myDate.getDate());
document.write("<br>");

// get Month part from the date string
document.write("Month : " + myDate.getMonth());
document.write("<br>");

// get Full Year from the date string
document.write("Year : " + myDate.getFullYear());
</script>

</body>
</html>
</head>

<body bgcolor="#FFFFFF">
<form name="form"  action="" method="post"><TR>

      <TD>要加的日期2:</TD>

      <TD><input type="text" name="u" value="2005-03-20" οnblur="agedem()" οnclick="agedem()"></TD>

      </TR>

      <TR>

      <TD>结果2:</TD>

     <TD><input name="twice"><br><inputname="age1"><input name="age2"/><inputname="age3"/><input name="age4"/><inputname="age5"/>日期差(年龄)<input name="age6"/></TD>

      </TR>--&gt;   </TABLE>

      </form>
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值