Home Web Front-end JS Tutorial Extended JavaScript Date method implementation code (prototype)_javascript skills

Extended JavaScript Date method implementation code (prototype)_javascript skills

May 16, 2016 pm 06:15 PM
date Expand

Recently, some functions of the project are being reconstructed, and the front-end has basically overturned the original design, and there are new solutions based on the accumulation in the previous six months. I have been refactoring and designing the front end these days and have encountered some problems. Because the main purpose of this module is to control time, it operates a large number of js Date objects, but there are too few native js Date methods, which is too inconvenient to operate. So I plan to extend the prototype of Date.

I have been engaged in C# development for a long time, and C# has influenced my thinking. The operation of DateTime in C# is very convenient, so I used it to extend the Date of js.

Copy code The code is as follows:

//Add the specified number of milliseconds to this instance Value on
Date.prototype.addMilliseconds = function (value) {
var millisecond = this.getMilliseconds();
this.setMilliseconds(millisecond value);
return this;
};
//Add the specified number of seconds to the value of this instance
Date.prototype.addSeconds = function (value) {
var second = this.getSeconds();
this.setSeconds( second value);
return this;
};
//Add the specified number of minutes to the value of this instance
Date.prototype.addMinutes = function (value) {
var minute = this.addMinutes();
this.setMinutes(minute value);
return this;
};
//Add the specified number of hours to the value of this instance
Date.prototype.addHours = function (value) {
var hour = this.getHours();
this.setHours(hour value);
return this;
};
// Adds the specified number of days to the value of this instance
Date.prototype.addDays = function (value) {
var date = this.getDate();
this.setDate(date value);
return this;
};
//Add the specified number of weeks to the value of this instance
Date.prototype.addWeeks = function (value) {
return this.addDays(value * 7);
};
//Add the specified number of months to the value of this instance
Date.prototype.addMonths = function (value) {
var month = this.getMonth() ;
this.setMonth(month value);
return this;
};
//Add the specified number of years to the value of this instance
Date.prototype.addYears = function (value) {
var year = this.getFullYear();
this.setFullYear(year value);
return this;
};
//Format date display format=" yyyy-MM-dd hh:mm:ss";
Date.prototype.format = function (format) {
var o = {
"M ": this.getMonth() 1, //month
"d ": this.getDate(), //day
"h ": this.getHours(), //hour
"m ": this.getMinutes(), //minute
"s ": this.getSeconds(), //second
"q ": Math.floor((this.getMonth() 3) / 3), //quarter
"S": this.getMilliseconds () //millisecond
}
if (/(y )/.test(format)) {
format = format.replace(RegExp.$1, (this.getFullYear() "").substr (4 - RegExp.$1.length));
}
for (var k in o) {
if (new RegExp("(" k ")").test(format)) {
format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" o[k]).substr(("" o[k]).length)) ;
}
}
return format;
}

I think there is no need to say more about how to use it, it is:
Copy code The code is as follows:

var date = new Date();
date.addHours(1);
date .addYears(2);
document.write(date.format('yyyy-MM-dd hh:mm:ss'));

I hope this extension method can help everyone.
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 use PHP's SNMP extension? How to use PHP's SNMP extension? Jun 02, 2023 am 10:22 AM

The SNMP extension for PHP is an extension that enables PHP to communicate with network devices through the SNMP protocol. Using this extension, you can easily obtain and modify the configuration information of network devices, such as CPU, memory, network interface and other information of routers, switches, etc. You can also perform control operations such as switching device ports. This article will introduce the basic knowledge of the SNMP protocol, how to install the SNMP extension of PHP, and how to use the SNMP extension in PHP to monitor and control network devices. 1. SN

From start to finish: How to use php extension cURL to make HTTP requests From start to finish: How to use php extension cURL to make HTTP requests Jul 29, 2023 pm 05:07 PM

From start to finish: How to use php extension cURL for HTTP requests Introduction: In web development, it is often necessary to communicate with third-party APIs or other remote servers. Using cURL to make HTTP requests is a common and powerful way. This article will introduce how to use PHP to extend cURL to perform HTTP requests, and provide some practical code examples. 1. Preparation First, make sure that php has the cURL extension installed. You can execute php-m|grepcurl on the command line to check

Extensions and third-party modules for PHP functions Extensions and third-party modules for PHP functions Apr 13, 2024 pm 02:12 PM

To extend PHP function functionality, you can use extensions and third-party modules. Extensions provide additional functions and classes that can be installed and enabled through the pecl package manager. Third-party modules provide specific functionality and can be installed through the Composer package manager. Practical examples include using extensions to parse complex JSON data and using modules to validate data.

PHP Warning: date() expects parameter 2 to be long, string given solution PHP Warning: date() expects parameter 2 to be long, string given solution Jun 22, 2023 pm 08:03 PM

When developing using PHP programs, you often encounter some warning or error messages. Among them, one error message that may appear is: PHPWarning:date()expectsparameter2tobelong,stringgiven. The error message means: the second parameter of the function date() is expected to be a long integer (long), but what is actually passed to it is a string (string). So, we

How to install mbstring extension under CENTOS7? How to install mbstring extension under CENTOS7? Jan 06, 2024 pm 09:59 PM

1.UncaughtError:Calltoundefinedfunctionmb_strlen(); When the above error occurs, it means that we have not installed the mbstring extension; 2. Enter the PHP installation directory cd/temp001/php-7.1.0/ext/mbstring 3. Start phpize(/usr/local/bin /phpize or /usr/local/php7-abel001/bin/phpize) command to install php extension 4../configure--with-php-config=/usr/local/php7-abel

How to use PHP's ZipArchive extension? How to use PHP's ZipArchive extension? Jun 02, 2023 am 08:13 AM

PHP is a popular server-side language that can be used to develop web applications and process files. The ZipArchive extension for PHP is a powerful tool for manipulating zip files in PHP. In this article, we’ll cover how to use PHP’s ZipArchive extension to create, read, and modify zip files. 1. Install the ZipArchive extension. Before using the ZipArchive extension, you need to ensure that the extension has been installed. The installation method is as follows: 1. Install

How to use the Aurora Push extension to implement batch message push function in PHP applications How to use the Aurora Push extension to implement batch message push function in PHP applications Jul 25, 2023 pm 08:07 PM

How to use the Aurora Push extension to implement batch message push function in PHP applications. In the development of mobile applications, message push is a very important function. Jiguang Push is a commonly used message push service that provides rich functions and interfaces. This article will introduce how to use the Aurora Push extension to implement batch message push functionality in PHP applications. Step 1: Register a Jiguang Push account and obtain an API key. First, we need to register on the Jiguang Push official website (https://www.jiguang.cn/push)

How to use PHP's POSIX extension? How to use PHP's POSIX extension? Jun 03, 2023 am 08:01 AM

The POSIX extensions for PHP are a set of functions and constants that allow PHP to interact with POSIX-compliant operating systems. POSIX (PortableOperatingSystemInterface) is a set of operating system interface standards designed to allow software developers to write applications that can run on various UNIX or UNIX-like operating systems. This article will introduce how to use POSIX extensions for PHP, including installation and use. 1. Install the POSIX extension of PHP in

See all articles