【Js】获取当前日期时间

日期基本数据: 

var myDate = new Date(); //Date 对象会自动把当前日期和时间保存为其初始值
myDate.getYear();  //返回当前年份(2位);请使用 getFullYear() 方法代替
myDate.getFullYear();  //返回完整的年份(4位,1970-????)
myDate.getMonth();  //返回当前月份(0-11,0表示1月)
myDate.getDate();  //返回当前日(1-31)
myDate.getDay(); //返回当前星期名(0-6,0表示星期天)
myDate.getHours(); //返回当前小时数(0-23)
myDate.getMinutes(); //返回当前分钟数(0-59)
myDate.getSeconds(); //返回当前秒数(0-59)
myDate.getMilliseconds(); //返回当前毫秒数(0-999)
myDate.getTime(); //返回1970年1月1日至今的毫秒数
myDate.toLocaleString(); //根据本地时间格式,把 Date 对象转换为字符串
myDate.toLocaleTimeString(); //根据本地时间格式,把 Date 对象的时间部分转换为字符串
myDate.toLocaleDateString(); //根据本地时间格式,把 Date 对象的日期部分转换为字符串

var nowDateFormat = myDate.getFullYear() + '-' + (myDate.getMonth() + 1) + '-' + myDate.getDate();// result:2021-2-20
//您可以使用名称数组,并使用 getMonth() 将月份作为名称返回:
var months = ["January", "February", "March", "April", "May", "June",
  "July", "August", "September", "October", "November", "December"];
var nowMonth = months[myDate.getMonth()]; //result:"February"
//您可以使用名称数组,并使用 getDay() 将星期名作为名称返回:
var days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
var nowWeekDay = days[myDate.getDay()]; //result:"Saturday"

日期相关算法: 

var nowDate = new Date();
var latestYear = nowDate.getFullYear();//当前年份
var latestMonth = nowDate.getMonth() + 1;//当前月份
var latestDay = new Date().getDate();//当前号数

//当前月总天数、当前月末尾号数
var latestMonthDay = new Date(latestYear, latestMonth, 0).getDate(); 
//上月总天数、上月末尾号数
var preMonthDay = new Date(latestYear, (latestMonth - 1), 0).getDate();
//今天
var today= latestYear + '-' + latestMonth + '-' + latestDay;
//今年年初
var currentYearStart = latestYear + '-01-01';
//本月初
var currentMonthStart = latestYear + '-' + latestMonth + '-01';
//本月末
var currentMonthEnd = latestYear + '-' + latestMonth + '-' + latestMonthDay;
//上月初
var preMonthStart = latestYear + '-' + (latestMonth - 1) + '-01';
//上月末
var preMonthEnd = latestYear + '-' + (latestMonth - 1) + '-' + preMonthDay;

相关推荐

  1. js获取当前时间

    2024-07-18 17:26:01       56 阅读
  2. js获取当前时间,当日零点,前一周时间

    2024-07-18 17:26:01       48 阅读
  3. PHP 日期时间 Date()函数 获取当前时间

    2024-07-18 17:26:01       34 阅读
  4. 安卓开发获取当前系统日期时间

    2024-07-18 17:26:01       26 阅读
  5. js或momentJs获取当前月的起止日期

    2024-07-18 17:26:01       60 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-18 17:26:01       66 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-18 17:26:01       70 阅读
  3. 在Django里面运行非项目文件

    2024-07-18 17:26:01       57 阅读
  4. Python语言-面向对象

    2024-07-18 17:26:01       68 阅读

热门阅读

  1. Fundamentals of Computer Science LCSCI4208

    2024-07-18 17:26:01       20 阅读
  2. 河南萌新联赛2024第(一)场:河南农业大学

    2024-07-18 17:26:01       27 阅读
  3. Unity:UI进入离开事件

    2024-07-18 17:26:01       19 阅读
  4. opencv—常用函数学习_“干货“_6

    2024-07-18 17:26:01       18 阅读
  5. web前端 Vue 框架面试120题(四)

    2024-07-18 17:26:01       18 阅读
  6. 富格林:可信办法阻挠虚假受骗

    2024-07-18 17:26:01       19 阅读
  7. ClickHouse中使用UNION

    2024-07-18 17:26:01       20 阅读
  8. vue3项目中pinia的用法详解(值得收藏)

    2024-07-18 17:26:01       20 阅读
  9. jd-gui反编译出现中文乱码问题

    2024-07-18 17:26:01       18 阅读
  10. CL11命令行解析使用实例

    2024-07-18 17:26:01       19 阅读
  11. PCB的层叠结构

    2024-07-18 17:26:01       18 阅读
  12. vim+cscope+ctags

    2024-07-18 17:26:01       23 阅读
  13. gitlab reset passwd

    2024-07-18 17:26:01       20 阅读
  14. 02-Redis未授权访问漏洞

    2024-07-18 17:26:01       21 阅读