广告
返回顶部
首页 > 资讯 > 前端开发 > JavaScript >使用js获取当前年月日的方法及格式整理汇总
  • 332
分享到

使用js获取当前年月日的方法及格式整理汇总

摘要

目录1. 获取完整时间戳2. 整理年月日数据为: 2022-02-09 的格式3. 关于日期数据的额外补充4. 判断闰年5.日期格式化总结 1. 获取完整时间戳 var date =

1. 获取完整时间戳

var date = new Date(); ====》 Wed Sep 07 2022 17:18:30 GMT+0800 (中国标准时间) 小节2中的数据以此为基础

2. 整理年月日数据为: 2022-02-09 的格式

var year = date.getFullYear();

var month = date.getMonth() + 1;

var day = date.getDate();

month = (month > 9) ? month : (“0” + month);
day = (day < 10) ? (“0” + day) : day;
var today = year + “-” + month + “-” + day;

3. 关于日期数据的额外补充

date.getYear(); // 获取当前年份(2 位)
date.getFullYear(); // 获取完整的年份(4 位, 1970-???)
date.getMonth(); // 获取当前月份(0-11,0 代表 1 月)
date.getDate(); // 获取当前日(1-31)
date.getDay(); // 获取当前星期 X(0-6,0 代表星期天)
date.getTime(); // 获取当前时间(从 1970.1.1 开始的毫秒数)
date.getHours(); // 获取当前小时数(0-23)
date.getMinutes(); // 获取当前分钟数(0-59)
date.getSeconds(); // 获取当前秒数(0-59)
date.getMilliseconds(); // 获取当前毫秒数(0-999)
date.toLocaleDateString(); // 获取当前日期
date.toLocaleTimeString(); // 获取当前时间
date.toLocaleString( ); // 获取日期与时间

4. 判断闰年

 var date1 = new Date();
 Date.prototype.isLeapYear = function() {   
    return (0==this.getYear()%4&&((this.getYear()%100!=0)||(this.getYear()%400==0)));   
 }   
 console.log(date1.isLeapYear()); // false

5.日期格式化


//---------------------------------------------------  
Date.prototype.FORMat = function(formatStr)  {   
    var str = formatStr;   
    var Week = ['日','一','二','三','四','五','六'];  
  
    str=str.replace(/yyyy|YYYY/,this.getFullYear());   
    str=str.replace(/yy|YY/,(this.getYear() % 100)>9?(this.getYear() % 100).toString():'0' + (this.getYear() % 100));   
  
    str=str.replace(/MM/,this.getMonth()>9?this.getMonth().toString():'0' + (this.getMonth()+1));      
    str=str.replace(/M/g,this.getMonth());   
 
 
    str=str.replace(/w|W/g,Week[this.getDay()]);   
  
    str=str.replace(/dd|DD/,this.getDate()>9?this.getDate().toString():'0' + this.getDate());   
    str=str.replace(/d|D/g,this.getDate());   
  
    str=str.replace(/hh|HH/,this.getHours()>9?this.getHours().toString():'0' + this.getHours());   
    str=str.replace(/h|H/g,this.getHours());   
    str=str.replace(/mm/,this.getMinutes()>9?this.getMinutes().toString():'0' + this.getMinutes());   
    str=str.replace(/m/g,this.getMinutes());   
  
    str=str.replace(/ss|SS/,this.getSeconds()>9?this.getSeconds().toString():'0' + this.getSeconds());   
    str=str.replace(/s|S/g,this.getSeconds());   
  
    return str;   
}   
var date=new Date();
var res=date.Format("yyyy-MM-dd HH:mm:ss  星期W");
console.info(res); //2022-01-07 16:18:36  星期五

总结

到此这篇关于使用js获取当前年月日的方法及格式整理汇总的文章就介绍到这了,更多相关js获取当前年月日及格式整理内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: 使用js获取当前年月日的方法及格式整理汇总

本文链接: https://www.lsjlt.com/news/174258.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作