js获取当前日期第几周

JavaScript012

js获取当前日期第几周,第1张

其实我是来混经验的

////////////////////////////////////////////////////////////////////////////////////////////////////

//获取当前日期在当前年第几周函数封装,例如2014-01-10 是当前年的第2周

////////////////////////////////////////////////////////////////////////////////////////////////////

function theWeek() {

var totalDays = 0

now = new Date()

years = now.getYear()

if (years <1000)

years += 1900

var days = new Array(12)

days[0] = 31

days[2] = 31

days[3] = 30

days[4] = 31

days[5] = 30

days[6] = 31

days[7] = 31

days[8] = 30

days[9] = 31

days[10] = 30

days[11] = 31

//判断是否为闰年,针对2月的天数进行计算

if (Math.round(now.getYear() / 4) == now.getYear() / 4) {

days[1] = 29

} else {

days[1] = 28

}

if (now.getMonth() == 0) {

totalDays = totalDays + now.getDate()

} else {

var curMonth = now.getMonth()

for (var count = 1count <= curMonthcount++) {

totalDays = totalDays + days[count - 1]

}

totalDays = totalDays + now.getDate()

}

//得到第几周

var week = Math.round(totalDays / 7)

return week

}

下面是获取当月的第几周

<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()+1var d = today.getDate()document.write( "今天是",m,"月的第 ", getMonthWeek(y, m, d), " 周" )</script>

通过getDay() 方法,可返回表示星期的某一天的数字。

星期中的某一天,使用本地时间。返回值是 0(周日) 到 6(周六) 之间的一个整数。

示例:

<script type="text/javascript">

var d=new Date()

var weekday=new Array(7)

weekday[0]="星期天"

weekday[1]="星期一"

weekday[2]="星期二"

weekday[3]="星期三"

weekday[4]="星期四"

weekday[5]="星期五"

weekday[6]="星期六"

document.write("Today it is " + weekday[d.getDay()])//返回当前时间是星期几

</script>

前言:需求里面有,做了就记录一下

第一种:获取当前月 当前周 的第一天 时分秒都为0,最后一天时分秒为23:59:59

ps:如果想获得指定日期的当前周,new Date('2020-1-2') 传参就可以了

//获取当前周

getTime(){

var date = new Date()

// 本周一的日期

date.setDate(date.getDate() - date.getDay() + 1)

var begin = date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate() + " 00:00:00"

// 本周日的日期

date.setDate(date.getDate() + 6)

var end = date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate() + " 23:59:59"

let timeInfo={

begin:begin,

end:end

}

return timeInfo

}

//获取当前月

getMtime(){

var data=new Date()

data.setDate(1)

data.setHours(0)

data.setSeconds(0)

data.setMinutes(0)

var start = data.getTime()

var currentMonth = data.getMonth()

var nextMonth = ++currentMonth

var nextMonthFirstDay = new Date(

data.getFullYear(),

nextMonth,

1

)

var end = nextMonthFirstDay-1

let timeInfo={

begin: this.timestampToTime(start),//这里调用时间戳转年月日时分秒方法

end: this.timestampToTime(end)

}

return timeInfo

}

//时间戳转年月日时分秒方法

timestampToTime (cjsj) {

var date = new Date(cjsj) //时间戳为10位需*1000,时间戳为13位的话不需乘1000

var Y = date.getFullYear() + '-'

var M = (date.getMonth()+1 <10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-'

var D = (date.getDate() <10 ? '0'+date.getDate() : date.getDate()) + ' '

var h = (date.getHours() <10 ? '0'+date.getHours() : date.getHours()) + ':'

var m = (date.getMinutes() <10 ? '0'+date.getMinutes() : date.getMinutes())+ ':'

var s = (date.getSeconds() <10 ? '0'+date.getSeconds() : date.getSeconds())

return Y+M+D+h+m+s

}