Home > Web Front-end > JS Tutorial > body text

How to get the specified date in Javascript

PHPz
Release: 2018-10-10 17:51:17
forward
1080 people have browsed it

这篇文章主要介绍了Javascript中获取指定日期的方法,感兴趣的朋友可以参考一下,希望对你有所帮助!

实现设计:

Step1:获取指定时间的日期数据

Step2:用当前起止时间进行搜索

代码实现:

1.定义日期格式化方法:

Date.prototype.Format = function (fmt) {
    var o = {
        "M+": this.getMonth() + 1, //月份
        "d+": this.getDate(), //日
        "h+": this.getHours(), //小时
        "m+": this.getMinutes(), //分
        "s+": this.getSeconds(), //秒
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度
        "S": this.getMilliseconds() //毫秒
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
    if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}
Copy after login

2.根据日期差获取指定日期数据:

function getdate(days) {
    var now = new Date();
    var date = new Date(now.getTime() - days * 24 * 3600 * 1000);
    var dates = date.Format('yyyy-MM-dd');
    return dates;
}
Copy after login

3.获取不同制定时间的日期数据并赋值给起始日历框:

function today_btn() {
    var today = getdate(0);
    var tomorrow = getdate(-1);
    document.getElementById("startdate").value = today;
    document.getElementById("enddate").value = tomorrow;
}

function yestoday_btn() {
    var today = getdate(0);
    var yestoday = getdate(1);
    document.getElementById("startdate").value = yestoday;
    document.getElementById("enddate").value = today;
}
Copy after login

接下来的事情就是用获取到的日期进行数据请求了。

更多相关教程亲访问  JavaScript视频教程

Related labels:
source:csdn.net
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!