Home >Web Front-end >JS Tutorial >Commonly used methods for dates in javascript_time and date

Commonly used methods for dates in javascript_time and date

WBOY
WBOYOriginal
2016-05-16 18:42:061076browse

1: Often involves date conversion comparison:

Copy code The code is as follows:

< html>










2: Date format judgment
Copy code The code is as follows:

//yyyy-MM-dd
if (!/^d{4}-dd?-dd?/.test(datavalue1)) {
alert("The date format of datevalue1 is illegal")
}
3: Convert week (extracted from others)
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 is 1---7: Sunday, Monday, Tuesday. . .
case 1:
showDate = "Sunday";
break;
case 2:
showDate = "Monday";
break;
case 3:
showDate = "Tuesday";
break;
case 4:
showDate = "Wednesday";
break;
case 5:
showDate = "Thursday";
break ;
case 6:
showDate = "Friday";
break;
default:
showDate = "Saturday";
break;
}
return showDate;
}
}
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn