首页
IT
登录
6mi
u
盘
搜
搜 索
IT
JS获取当天、本周、本月、本年的时间
JS获取当天、本周、本月、本年的时间
xiaoxiao
2021-03-25
190
<!DOCTYPE html>
<
html
lang
=
"en"
>
<
head
>
<
meta
charset
=
"UTF-8"
>
<
title
>
Document
</
title
>
</
head
>
<
body
>
<
input
type
=
"text"
id
=
"dateFrom"
value
=
""
>
<
input
type
=
"text"
id
=
"dateTo"
value
=
""
>
<
script
>
window.onload =
function
()
{
// getDate("thisDay");
// getDate("thisWeek");
// getDate("thisMonth");
getDate(
"thisYear"
); }
//获取推送时间段
function
getDate
(type)
{
var
kssj,jssj;
var
today =
new
Date
();
var
DAY =
1000
*
60
*
60
*
24
;
if
(type ==
"thisDay"
) {
//当天
kssj =
new
Date
(); jssj =
new
Date
(); }
if
(type ==
"thisWeek"
) {
//本周周
kssj = getThisWeekFirstDay();
//本周第一天
jssj =
new
Date
(); }
if
(type ==
"thisMonth"
) {
//本月
kssj = getThisMonthFirstDay();
//本月第一天
jssj =
new
Date
(); }
if
(type ==
"thisYear"
) { kssj = getThisYearFirstDay();
//本年第一天
jssj =
new
Date
(); }
var
dateFrom = kssj.format(
'yyyy-MM-dd'
);
var
dateTo = jssj.format(
'yyyy-MM-dd'
); document.getElementById(
"dateFrom"
).value=dateFrom; document.getElementById(
"dateTo"
).value=dateTo; }
function
getThisWeekFirstDay
()
{
var
now =
new
Date
();
var
day = now.getDay();
var
week =
"1234567"
;
var
first =
0
- week.indexOf(day);
var
thisWeekfirstDay =
new
Date
; thisWeekfirstDay.setDate (thisWeekfirstDay.getDate () + first);
return
thisWeekfirstDay; }
function
getThisMonthFirstDay
()
{
var
date=
new
Date
(); date.setDate(
1
);
return
date; }
function
getThisYearFirstDay
()
{
var
currentDate=
new
Date
();
var
currentYear=currentDate.getFullYear();
var
currentYearFirstDate=
new
Date
(currentYear,
0
,
1
);
return
currentYearFirstDate; }
//data类型转string
Date
.prototype.format =
function
(format)
{
var
o = {
"M+"
:
this
.getMonth()+
1
,
//month
"d+"
:
this
.getDate(),
//day
"h+"
:
this
.getHours(),
//hour
"m+"
:
this
.getMinutes(),
//minute
"s+"
:
this
.getSeconds(),
//second
"q+"
:
Math
.floor((
this
.getMonth()+
3
)/
3
),
//quarter
"S"
:
this
.getMilliseconds()
//millisecond
}
if
(
/(y+)/
.test(format)) format=format.replace(
RegExp
.$
1
, (
this
.getFullYear()+
""
).substr(
4
-
RegExp
.$
1.
length));
for
(
var
k
in
o)
if
(
new
RegExp
(
"("
+ k +
")"
).test(format)) format = format.replace(
RegExp
.$
1
,
RegExp
.$
1.
length==
1
? o[k] : (
"00"
+ o[k]).substr((
""
+ o[k]).length));
return
format; }
</
script
>
</
body
>
</
html
>
转载请注明原文地址: https://ju.6miu.com/read-973.html
技术
最新回复
(
0
)