Home > Web Front-end > JS Tutorial > JS simple method to get the current year, month, day and week

JS simple method to get the current year, month, day and week

不言
Release: 2018-05-05 16:35:04
Original
1713 people have browsed it

This article mainly introduces the method of JS to simply obtain the current year, month, day and week, and analyzes the method of obtaining the current date and time in javascript based on a custom function in the form of a complete example, involving the use of the Date() class in javascript and date-related Friends who need it can refer to

for calculation skills. This article describes a simple method of obtaining the current year, month, day and week using JS. Share it with everyone for your reference, the details are as follows:

<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>获取年月日星期</title>
<script type="text/javascript" language="javascript" >
function getDateWeek()
{
todayDate = new Date();
date = todayDate.getDate();
month= todayDate.getMonth() +1;
year= todayDate.getYear();
var dateweek = "今天是:";
if(navigator.appName == "Netscape")
{
  dateweek = dateweek + (1900+year) + "年" + month + "月" + date + "日 ";
}
if(navigator.appVersion.indexOf("MSIE") != -1)
{
  dateweek = dateweek + year + "年" + month + "月" + date + "日 ";
}
switch(todayDate.getDay())
{
case 0:dateweek += "星期日";break;
case 1:dateweek += "星期一";break;
case 2:dateweek += "星期二";break;
case 3:dateweek += "星期三";break;
case 4:dateweek += "星期四";break;
case 5:dateweek += "星期五";break;
case 6:dateweek += "星期六";break;
}
return dateweek;
}
document.write(getDateWeek());
</script>
</head>
<body >
</body>
</html>
Copy after login

The operation effect diagram is as follows:

Related Recommendation:

JS simple implementation of floating window

JS simple implementation of return to the previous page concise introduction

JS Analysis of simple methods to achieve array deduplication

The above is the detailed content of JS simple method to get the current year, month, day and week. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template