This article brings you some weird date conversions in JavaScript. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
【Recommended reading: How to convert date format in JS】
1. Get today’s 0 hour 0 minutes and 0 seconds (commonly used to obtain the start date)
new Date(new Date()。toLocaleDateString()); // Mon Nov 12 2018 00:00:00 GMT+0800 (中国标准时间)
2. Get the date one month ago
new Date(new Date()。setMonth(new Date()。getMonth()-1)); //Fri Oct 12 2018 17:20:07 GMT+0800 (中国标准时间)
3. Get the 0 hours, 0 minutes and 0 seconds one month ago
new Date(new Date(new Date()。toLocaleDateString())。setMonth(new Date()。getMonth()-1)); //Fri Oct 12 2018 00:00:00 GMT+0800 (中国标准时间)
4. Get the previous day’s date
new Date(new Date()。setDate(new Date()。getDate()-1)); // Sun Nov 11 2018 17:21:56 GMT+0800 (中国标准时间)
5. Add one day
var dateTime=new Date(); dateTime=dateTime.setDate(dateTime.getDate()+1); dateTime=new Date(dateTime); // Tue Nov 13 2018 17:24:32 GMT+0800 (中国标准时间)
6. Subtract one day
var dateTime=new Date(); dateTime=dateTime.setDate(dateTime.getDate()-1); dateTime=new Date(dateTime); // Sun Nov 11 2018 17:25:12 GMT+0800 (中国标准时间)
7. Get today’s 23:59:59 Seconds
new Date(new Date(new Date()。toLocaleDateString())。getTime()+24*60*60*1000-1); // Mon Nov 12 2018 23:59:59 GMT+0800 (中国标准时间)
Get yesterday’s 23:59:59
var dateTime = new Date() dateTime = dateTime.setDate(dateTime.getDate() -1 ) dateTime = new Date(new Date(new Date(dateTime)。toLocaleDateString())。getTime() + 24 * 60 * 60 * 1000 - 1) // Sun Nov 11 2018 23:59:59 GMT+0800 (中国标准时间)
8. Get tomorrow’s 23:59:59
var dateTime = new Date() dateTime = dateTime.setDate(dateTime.getDate() + 1 ) dateTime = new Date(new Date(new Date(dateTime)。toLocaleDateString())。getTime() + 24 * 60 * 60 * 1000 - 1) // Tue Nov 13 2018 23:59:59 GMT+0800 (中国标准时间)
Get tomorrow’s 23:00 of the current time 59 minutes and 59 seconds
var curTime = 'Wed Nov 14 2018 17:38:31 GMT+0800 (中国标准时间)'; var dateTime = new Date(curTime) dateTime = dateTime.setDate(dateTime.getDate() + 1 ) dateTime = new Date(new Date(new Date(dateTime)。toLocaleDateString())。getTime() + 24 * 60 * 60 * 1000 - 1) // Thu Nov 15 2018 23:59:59 GMT+0800 (中国标准时间)
9. Get yesterday’s current time of 23 hours, 59 minutes and 59 seconds
var curTime = 'Wed Nov 14 2018 17:38:31 GMT+0800 (中国标准时间)'; var dateTime = new Date(curTime) dateTime = dateTime.setDate(dateTime.getDate() - 1 ) dateTime = new Date(new Date(new Date(dateTime)。toLocaleDateString())。getTime() + 24 * 60 * 60 * 1000 - 1) // Tue Nov 13 2018 23:59:59 GMT+0800 (中国标准时间)
10. How many hours, minutes and seconds are left before the event ends (countdown)
var endTime = function (startTime) { setInterval( function(){ var str = ''; var date1 = new Date(startTime); //开始时间 var date2 = new Date(); //结束时间 var date3 = date1.getTime() - date2.getTime(); //时间差的毫秒数 if (date3>0){ // //计算相差的年数 // var years = Math.floor(date3 / (12 * 30 * 24 * 3600 * 1000)); // //计算相差的月数 var leave = date3 % (12 * 30 * 24 * 3600 * 1000); // var months = Math.floor(leave / (30 * 24 * 3600 * 1000)); // //计算出相差天数 var leave0 = leave % (30 * 24 * 3600 * 1000); var days = Math.floor(date3 / (24 * 3600 * 1000)); //计算出小时数 var leave1 = leave0 % (24 * 3600 * 1000); //计算天数后剩余的毫秒数 var hours = Math.floor(leave1 / (3600 * 1000)); //计算相差分钟数 var leave2 = leave1 % (3600 * 1000); //计算小时数后剩余的毫秒数 var minutes = Math.floor(leave2 / (60 * 1000));
The above is a complete introduction to some weird date conversions in JavaScript. If you want to know more about JavaScript Tutorial, please pay attention to the PHP Chinese website.
The above is the detailed content of Some weird date conversions in javascript. For more information, please follow other related articles on the PHP Chinese website!

去掉重复并排序的方法:1、使用“Array.from(new Set(arr))”或者“[…new Set(arr)]”语句,去掉数组中的重复元素,返回去重后的新数组;2、利用sort()对去重数组进行排序,语法“去重数组.sort()”。

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于Symbol类型、隐藏属性及全局注册表的相关问题,包括了Symbol类型的描述、Symbol不会隐式转字符串等问题,下面一起来看一下,希望对大家有帮助。

怎么制作文字轮播与图片轮播?大家第一想到的是不是利用js,其实利用纯CSS也能实现文字轮播与图片轮播,下面来看看实现方法,希望对大家有所帮助!

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于对象的构造函数和new操作符,构造函数是所有对象的成员方法中,最早被调用的那个,下面一起来看一下吧,希望对大家有帮助。

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于面向对象的相关问题,包括了属性描述符、数据描述符、存取描述符等等内容,下面一起来看一下,希望对大家有帮助。

方法:1、利用“点击元素对象.unbind("click");”方法,该方法可以移除被选元素的事件处理程序;2、利用“点击元素对象.off("click");”方法,该方法可以移除通过on()方法添加的事件处理程序。

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于BOM操作的相关问题,包括了window对象的常见事件、JavaScript执行机制等等相关内容,下面一起来看一下,希望对大家有帮助。

本篇文章整理了20+Vue面试题分享给大家,同时附上答案解析。有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

Notepad++7.3.1
Easy-to-use and free code editor

Atom editor mac version download
The most popular open source editor

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.
