javascript 日期常用的方法

來源:互聯網
上載者:User

1:經常涉及到日期的換算比較: 複製代碼 代碼如下:<html>
<head>
<title></title>
<script language="javascript">
function returnMaxDate(){
var datevalue1 = document.getElementById("date1").value;
var datevalue2 = document.getElementById("date2").value;
if(datevalue1 > datevalue2){
return datevalue1;
}else{
return datevalue2;
}
}
</script>
</head>
<body>
<input type="text" name="date1" id="date1">
<input type="text" name="date2" id="date2">
</body>
</html>

2:日期的格式判斷 複製代碼 代碼如下://yyyy-MM-dd
if (!/^\d{4}\-\d\d?\-\d\d?/.test(datavalue1)) {
alert("datevalue1的日期格式不合法")
}
3:換算星期(摘抄他人)
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class TestDate {
public static String getWeekFromDate(String sDate,int type){
if("".equals(sDate)){
return "";
}
SimpleDateFormat df = null;
if(type==0){
df = new SimpleDateFormat("yyyy-MM-dd");
}else if(type==1){
df = new SimpleDateFormat("yyyyMMdd");
}
Date date = null;
try{
date = df.parse(sDate);
}catch(ParseException e){
e.printStackTrace();
}
Calendar cd = Calendar.getInstance();
cd.setTime(date);
int mydate = cd.get(Calendar.DAY_OF_WEEK);
String showDate = "";
switch (mydate) { //mydate分別是 1---7:星期日,星期一,星期二。。。
case 1:
showDate = "星期日";
break;
case 2:
showDate = "星期一";
break;
case 3:
showDate = "星期二";
break;
case 4:
showDate = "星期三";
break;
case 5:
showDate = "星期四";
break;
case 6:
showDate = "星期五";
break;
default:
showDate = "星期六";
break;
}
return showDate;
}
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.