Home > Web Front-end > JS Tutorial > js method to get the current time and the week of this month_javascript skills

js method to get the current time and the week of this month_javascript skills

WBOY
Release: 2016-05-16 15:45:40
Original
1388 people have browsed it

The example in this article describes the js implementation method of obtaining the current week of the month. Share it with everyone for your reference. The details are as follows:

<script language="javascript">
var getMonthWeek = function (a, b, c) { 
/* 
a = d = 当前日期 
b = 6 - w = 当前周的还有几天过完(不算今天) 
a + b 的和在除以7 就是当天是当前月份的第几周 
*/ 
var date = new Date(a, parseInt(b) - 1, c), w = date.getDay(), d = date.getDate(); 
return Math.ceil( 
(d + 6 - w) / 7 
); 
};
var getYearWeek = function (a, b, c) { 
/* 
date1是当前日期 
date2是当年第一天 
d是当前日期是今年第多少天 
用d + 当前年的第一天的周差距的和在除以7就是本年第几周 
*/ 
var date1 = new Date(a, parseInt(b) - 1, c), date2 = new Date(a, 0, 1), 
d = Math.round((date1.valueOf() - date2.valueOf()) / 86400000); 
return Math.ceil( 
(d + ((date2.getDay() + 1) - 1)) / 7 
); 
}; 
today=new Date();//获取当前时间
var y = today.getYear();
var m = today.getMonth()+1;
var d = today.getDate();
document.write( "今天是",m,"月的第 ", getMonthWeek(y, m, d), " 周" ); 
</script>

Copy after login

I hope this article will be helpful to everyone’s JavaScript programming design.

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