zoukankan      html  css  js  c++  java
  • javascript 红宝书笔记之操作日期

    • 创建当日 日期对象
     
    调用Date的构造函数而不传递参数的情况下,新创建的对象默认获取当前的日期和时间。
     
    var now = new Date();
     
    • 创建特定的日期和时间对象
     
    Date.parse("表示日期的字符串");                    //该方法接收一个表示日期的字符串参数,根据这个字符串会返回相应的日期格式;如果这个字符串不能表示日期,则会返回NAN。
     
    var nowDate = new Date(Date.parse("May 25, 2001"));      //May 25,2001
    var nowDate = new Date("May 25,2001");                   //May 25,2001
     
    • 取得开始时间
     
    var start = Date.now();
     
    • 取得结束时间
     
    var stop = Date.now();
    result = stop - start;                                  //result 结果
     
    • 日期格式化方法,就是将日期格式化 为字符串
     
    toDateString()    以特定的格式显示星期几、月、日、年
    toTimeString()    以特定的格式显示时、分、秒
    toLocaleDateString()    显示本地日期
    toLocaleTimeString()    显示本地时间
     
    • 日期组件方法
     
    getTime()                 与valueOf()返回的日期相同,返回的是毫秒数
    setTime()                 以毫秒数设置日期,会改变整个日期
     
    getYear()                 返回2位数的年份
    getFullYear()             返回4位数的年份
    setFullYear()             设置4位数的年份
     
    getMonth()                返回月份
    setMonth()                设置月份,月份必须大于0,超过11则增加月份
     
    getDate()                 返加日期(1~31)
    setDate()                 设置月份中的天数,如果传入的值超过该月中应用的天数,则增加月份
     
    getDay()                  返回日期中的星期几
     
    getHours()                返回日期中的小时数
    setHours()                设置日期中的小时数,大于23则增加月份中的天数
     
    getMinutes()              返回日期中的分钟数
    setMinutes()              设置日期中的分钟数,大于59则增加小时数
     
    • 获取当前日期和时间,并按照YYYY-MM-DD格式化日期
     
    获取当前日期
    var nowDate = new Date();
     
    nowDate.getFullYear();
    nowDate.getMonth() + 1;
    nowDate.getDate();
    nowDate.getHours();
    nowDate.getMinutes();
    nowDate.getSeconds();
     
    var formatDate = function(nowDate){
         var year = nowDate.getFullYear();
         var month = nowDate.getMonth() + 1 ;
         month = month < 10 ? "0" + month : month;
         var date = nowDate.getDate();
         date = date < 10 ? "0" + date : date;
         
         return year + "-" + month + "-" + date;
    }
    var nowStr = nowDate.format("yyyy-mm-dd");
    var nowStr = nowDate.format("yyyy-mm-dd hh:mm:ss");
    var nowStr = nowDate.format("yyyy年mm月dd日");
    var nowStr = nowDate.format("yyyy / mm / dd");
  • 相关阅读:
    面向对象的继承关系体现在数据结构上时,如何表示
    codeforces 584C Marina and Vasya
    codeforces 602A Two Bases
    LA 4329 PingPong
    codeforces 584B Kolya and Tanya
    codeforces 584A Olesya and Rodion
    codeforces 583B Robot's Task
    codeforces 583A Asphalting Roads
    codeforces 581C Developing Skills
    codeforces 581A Vasya the Hipster
  • 原文地址:https://www.cnblogs.com/baiyygynui/p/5562855.html
Copyright © 2011-2022 走看看