search
HomeWeb Front-endJS TutorialSome weird date conversions in javascript

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

Some weird date conversions in javascript

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!

Statement
This article is reproduced at:segmentfault. If there is any infringement, please contact admin@php.cn delete
Python vs. JavaScript: A Comparative Analysis for DevelopersPython vs. JavaScript: A Comparative Analysis for DevelopersMay 09, 2025 am 12:22 AM

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Python vs. JavaScript: Choosing the Right Tool for the JobPython vs. JavaScript: Choosing the Right Tool for the JobMay 08, 2025 am 12:10 AM

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript: Understanding the Strengths of EachPython and JavaScript: Understanding the Strengths of EachMay 06, 2025 am 12:15 AM

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScript's Core: Is It Built on C or C  ?JavaScript's Core: Is It Built on C or C ?May 05, 2025 am 12:07 AM

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript Applications: From Front-End to Back-EndJavaScript Applications: From Front-End to Back-EndMay 04, 2025 am 12:12 AM

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Python vs. JavaScript: Which Language Should You Learn?Python vs. JavaScript: Which Language Should You Learn?May 03, 2025 am 12:10 AM

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

JavaScript Frameworks: Powering Modern Web DevelopmentJavaScript Frameworks: Powering Modern Web DevelopmentMay 02, 2025 am 12:04 AM

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

The Relationship Between JavaScript, C  , and BrowsersThe Relationship Between JavaScript, C , and BrowsersMay 01, 2025 am 12:06 AM

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment