美文网首页
#JS#Date()速查

#JS#Date()速查

作者: a5c0a9d9ccb8 | 来源:发表于2016-05-24 17:43 被阅读36次
  1. 新建
  • new Date()
  • new Date(毫秒数)
  • new Date(标准时间格式字符串)
  • new Date(年, 月, 日, 时, 分, 秒, 毫秒)
  1. 获取
var muDate = new Date();
//方法
myDate.getYear();//获取当前年份(2位)
myDate.getFullYear(); //获取完整的年份(4位,1970-????)
myDate.getMonth(); //获取当前月份(0-11,0代表1月)
myDate.getDate(); //获取当前日(1-31)
myDate.getDay(); //获取当前星期X(0-6,0代表星期天)
myDate.getTime(); //获取当前时间(从1970.1.1开始的毫秒数)
myDate.getHours(); //获取当前小时数(0-23)
myDate.getMinutes(); //获取当前分钟数(0-59)
myDate.getSeconds(); //获取当前秒数(0-59)
myDate.getMilliseconds(); //获取当前毫秒数(0-999)
myDate.toLocaleDateString(); //获取当前日期var 
mytime=myDate.toLocaleTimeString(); //获取当前时间
myDate.toLocaleString( ); //获取日期与时间
  1. 自定义方法
/*
*格式化:传入Date()对象得到yyyy-mm-dd
*/
function getFormatDate(date){
    var month,day;
    if((date.getMonth() + 1) < 10){
        month = "0" + (date.getMonth() + 1);
    }else{
        month = date.getMonth() + 1;
    }
    if(date.getDate() < 10){
        day = "0" + date.getDate();
    }else{
        day = date.getDate();
    }
    return date.getFullYear() + "-" + month + "-" + day;
}
/*
*日期计算/限制:计算指定日期n天后的日期
*/
function getDay(date,n){
       return (new Date(+date + 1000*60*60*24*n));
   }
  1. 注意点
  • 月份是从0开始的,所以需要+1才是正确的
  • 星期天对应0

献给我的小石头

相关文章

网友评论

      本文标题:#JS#Date()速查

      本文链接:https://www.haomeiwen.com/subject/mdyzrttx.html