Table of Contents
获取某个月份的天数
获取时区
计算运行时间
删除cookie" >删除cookie
Home Web Front-end JS Tutorial Detailed introduction to JavaScript date operation methods

Detailed introduction to JavaScript date operation methods

Mar 15, 2017 pm 02:37 PM

日期操作在JavaScript开发中经常会遇到,尤其是对初学者来说,JavaScript的日期操作比较繁琐,但是必须掌握常用的日期操作方法,本文就简单介绍一下JavaScript操作日期类型的常用方法,希望能给你有所帮助。

获取某个月份的天数

相信大家读小学的时候就知道一年十二个月各有多少天了,这里面有个特殊的存在——2月。闰年的2月有29天,非闰年的2月只有28天。估计不少人跟我一样,已经不记得闰年的规则了,这时候,下面的这个方法就派上用场了。

var date = new Date(2013, 2, 0);
date.getDate();  // 28
date = new Date(2012, 2, 0);
date.getDate();  // 29
Copy after login

创建Date对象时可以传入三个参数,分别是年、月(0~11,0表示一月)、日,如果日的参数为0,那创建出来的对象表示的就是上个月的最后一天,如此就可以知道上个月有多少天了。

同样的,我们也可以通过这个方法判断某年是否闰年:

function isLeapYear(year) {
    return new Date(year, 2, 0).getDate() === 29;
}
isLeapYear(2012);  // true
Copy after login

获取时区

日期类型的 getTimezoneOffset() 方法可以获取格林威治时间和本地时间之间的时间差,以分钟为单位。例如:

var date = new Date();
var timezoneOffset = date.getTimezoneOffset(); // 中国(东八区)为-480
-timezoneOffset / 60;  // 8
Copy after login

把获取到的时间差除以60,再取负值就是所在的时区了。

除此以外,还有一个方法。调用日期类型的 toString() 后,可以得到一段固定格式的日期字符串

new Date().toString(); // Sun Mar 10 2013 16:41:12 GMT+0800 (中国标准时间)
Copy after login

显而易见,GMT后面的+800就是我们要的时区了,只要通过正则表达式匹配一下就可以拿到该值。

/GMT([+-]\d+)/.test( new Date().toString() );
var timezone = RegExp.$1;  // +0800
Copy after login

不过此时的 timezone 变量是字符串,如果要转成数字类型,还要进行一些处理。

计算运行时间

如何测量某段程序的执行时间呢?方法很简单,在执行前记录一次时间,执行后用当前时间减去执行前的时间,就得到结果了:

var startTime = new Date();
// some program
console.log(new Date() - startTime);
Copy after login

这里无需手动把日期转换为数字,因为进行减法运算的时候自然会强制转换。这样算出来的结果是毫秒级的,精度不是很够,不过对浏览器端的Javascript来说,也没必要纠结于1毫秒以内的消耗了。

准确地说,我们没法直接通过Javascript删除cookie。要想把某个cookie从这个世界抹杀掉,唯一的办法就是让它过期,这样浏览器的内建机制就会把它自动干掉。

而要让cookie过期,最直截了当的方法就是把它的过期时间设为最小值。Javascript里面能表示的最小日期就是1970年1月1日0时0点0分,通过 new Date(0) 就可以创建出这样一个日期对象:

var cookieName = 'name'; // cookie名
document.cookie = cookieName + '=' + '; expires=' + new Date(0).toUTCString();
Copy after login

The above is the detailed content of Detailed introduction to JavaScript date operation methods. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

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

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to implement an online speech recognition system using WebSocket and JavaScript How to implement an online speech recognition system using WebSocket and JavaScript Dec 17, 2023 pm 02:54 PM

How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems WebSocket and JavaScript: key technologies for implementing real-time monitoring systems Dec 17, 2023 pm 05:30 PM

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

How to use JavaScript and WebSocket to implement a real-time online ordering system How to use JavaScript and WebSocket to implement a real-time online ordering system Dec 17, 2023 pm 12:09 PM

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

How to implement an online reservation system using WebSocket and JavaScript How to implement an online reservation system using WebSocket and JavaScript Dec 17, 2023 am 09:39 AM

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

JavaScript and WebSocket: Building an efficient real-time weather forecasting system JavaScript and WebSocket: Building an efficient real-time weather forecasting system Dec 17, 2023 pm 05:13 PM

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

Simple JavaScript Tutorial: How to Get HTTP Status Code Simple JavaScript Tutorial: How to Get HTTP Status Code Jan 05, 2024 pm 06:08 PM

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

How to use insertBefore in javascript How to use insertBefore in javascript Nov 24, 2023 am 11:56 AM

Usage: In JavaScript, the insertBefore() method is used to insert a new node in the DOM tree. This method requires two parameters: the new node to be inserted and the reference node (that is, the node where the new node will be inserted).

JavaScript and WebSocket: Building an efficient real-time image processing system JavaScript and WebSocket: Building an efficient real-time image processing system Dec 17, 2023 am 08:41 AM

JavaScript is a programming language widely used in web development, while WebSocket is a network protocol used for real-time communication. Combining the powerful functions of the two, we can create an efficient real-time image processing system. This article will introduce how to implement this system using JavaScript and WebSocket, and provide specific code examples. First, we need to clarify the requirements and goals of the real-time image processing system. Suppose we have a camera device that can collect real-time image data

See all articles