search
HomeWeb Front-endJS Tutorialjs time function application addition, subtraction, comparison, format conversion sample code_javascript skills

Copy code The code is as follows:

// JavaScript Document
//---------------------------------- ----------------
// Determining leap years
//------------------------ -----------------------------
Date.prototype.isLeapYear = function() {
return (0== this.getYear()%4&&((this.getYear() 0!=0)||(this.getYear()@0==0)));
}

//- --------------------------------------------------
// Date formatting
// Format YYYY/yyyy/YY/yy represents year
// MM/M month
// W/w week
// dd/DD/ d/D Date
// hh/HH/h/H Time
// mm/m Minutes
// ss/SS/s/S Seconds
//------ ---------------------------------------------
// Extension of Date, convert Date into String in the specified format
// month (M), day (d), hour (h), minute (m), second (s), quarter (q) can use 1 -2 placeholders,
// Year (y) can use 1-4 placeholders, milliseconds (S) can only use 1 placeholder (a number with 1-3 digits)
// Example:
// (new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423
// (new Date()).Format("yyyy-M-d h:m:s.S") ==> 2006-7-2 8:9:4.18
Date.prototype.Format = function(fmt)
{ //author: meizz
var o = {
"M " : this.getMonth() 1, //Month
"d " : this.getDate(), //Day
"h " : this.getHours(), //Hours
"H " : this.getHours(), //Hours
"m " : this.getMinutes(), //Minutes
" s " : this.getSeconds(), //Seconds
"q " : Math.floor((this.getMonth() 3)/3), //Quarter
"S" : this.getMilliseconds() //Milliseconds
};
if(/(y )/.test(fmt))
fmt=fmt.replace(RegExp.$1, (this.getFullYear() "").substr(4 - RegExp.$1.length));
for(var k in o)
if(new RegExp("(" k ")").test(fmt))
fmt = fmt.replace( RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00" o[k]).substr(("" o[k]).length)));
return fmt;
}

/**
* Extension of Date, convert Date into String in specified format
* Month (M), day (d), 12 hours (h), 24 hours (H), minutes (m), seconds ( s), week (E), quarter (q) can use 1-2 placeholders
* year (y) can use 1-4 placeholders, milliseconds (S) can only use 1 placeholder symbol (is a number of 1-3 digits)
* eg:
* (new Date()).pattern("yyyy-MM-dd hh:mm:ss.S") ==> 2006- 07-02 08:09:04.423
* (new Date()).pattern("yyyy-MM-dd E HH:mm:ss") ==> 2009-03-10 Tuesday 20:09:04
* (new Date()).pattern("yyyy-MM-dd EE hh:mm:ss") ==> 2009-03-10 Tuesday 08:09:04
* (new Date( )).pattern("yyyy-MM-dd EEE hh:mm:ss") ==> 2009-03-10 Tuesday 08:09:04
* (new Date()).pattern("yyyy- M-d h:m:s.S") ==> 2006-7-2 8:9:4.18
*/
Date.prototype.pattern=function(fmt) {
var o = {
"M " : this.getMonth() 1, //Month
"d " : this.getDate(), //Day
"h " : this.getHours() == 0 ? 12 : this.getHours( ), //hours
"H " : this.getHours(), //hours
"m " : this.getMinutes(), //minutes
"s " : this.getSeconds() , //Seconds
"q " : Math.floor((this.getMonth() 3)/3), //Quarter
"S" : this.getMilliseconds() //Milliseconds
};
var week = {
"0" : "/u65e5",
"1" : "/u4e00",
"2" : "/u4e8c",
"3" : "/u4e09",
"4" : "/u56db",
"5" : "/u4e94",
"6" : "/u516d"
};
if( /(y )/.test(fmt)){
fmt=fmt.replace(RegExp.$1, (this.getFullYear() "").substr(4 - RegExp.$1.length));
}
if(/(E )/.test(fmt)){
fmt=fmt.replace(RegExp.$1, ((RegExp.$1.length>1) ? (RegExp.$1.length>2 ? "/u661f/u671f" : "/u5468") : "") week[this.getDay() ""]);
}
for(var k in o){
if(new RegExp("(" k ")").test(fmt)){
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (( "00" o[k]).substr(("" o[k]).length)));
}
}
return fmt;
}

/ /------------------------------------------------- --
//| Find the difference in days between two times. The date format is YYYY-MM-dd
// ----------------------- -----------------------------
function daysBetween(DateOne,DateTwo)
{
var OneMonth = DateOne .substring(5,DateOne.lastIndexOf ('-'));
var OneDay = DateOne.substring(DateOne.length,DateOne.lastIndexOf ('-') 1);
var OneYear = DateOne.substring( 0,DateOne.indexOf ('-'));

var TwoMonth = DateTwo.substring(5,DateTwo.lastIndexOf ('-'));
var TwoDay = DateTwo.substring(DateTwo.length ,DateTwo.lastIndexOf ('-') 1);
var TwoYear = DateTwo.substring(0,DateTwo.indexOf ('-'));
var cha=((Date.parse(OneMonth '/' OneDay '/' OneYear)- Date.parse(TwoMonth '/' TwoDay '/' TwoYear))/86400000);
return Math.abs(cha);
}

// ---------------------------------------------------
//| 日期计算
// ---------------------------------------------------
Date.prototype.DateAdd = function(strInterval, Number) {
var dtTmp = this;
switch (strInterval) {
case 's' :return new Date(dtTmp.getFullYear(), (dtTmp.getMonth()), dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(), dtTmp.getSeconds() Number); //秒
case 'n' :return new Date(dtTmp.getFullYear(), (dtTmp.getMonth()), dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes() Number, dtTmp.getSeconds()); //分
case 'h' :return new Date(dtTmp.getFullYear(), (dtTmp.getMonth()), dtTmp.getDate(), dtTmp.getHours() Number, dtTmp.getMinutes(), dtTmp.getSeconds()); //时
case 'd' :return new Date(dtTmp.getFullYear(), (dtTmp.getMonth()), dtTmp.getDate() Number, dtTmp.getHours(), dtTmp.getMinutes(), dtTmp.getSeconds()); //天
case 'w' :return new Date(dtTmp.getFullYear(), (dtTmp.getMonth()), dtTmp.getDate() Number*7, dtTmp.getHours(), dtTmp.getMinutes(), dtTmp.getSeconds()); //周
case 'q' :return new Date(dtTmp.getFullYear(), (dtTmp.getMonth()) Number*3, dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(), dtTmp.getSeconds());//季度
case 'm' :return new Date(dtTmp.getFullYear(), (dtTmp.getMonth()) Number, dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(), dtTmp.getSeconds()); //月
case 'y' :return new Date((dtTmp.getFullYear() Number), dtTmp.getMonth(), dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(), dtTmp.getSeconds()); //年
}
}

// ---------------------------------------------------
//| 比较日期差 dtEnd 格式为日期型或者 有效日期格式字符串
// ---------------------------------------------------
Date.prototype.DateDiff = function(strInterval, dtEnd) {
var dtStart = this;
if (typeof dtEnd == 'string' )//如果是字符串转换为日期型
{
dtEnd = StringToDate(dtEnd);
}
switch (strInterval) {
case 's' :return parseInt((dtEnd - dtStart) / 1000);
case 'n' :return parseInt((dtEnd - dtStart) / 60000);
case 'h' :return parseInt((dtEnd - dtStart) / 3600000);
case 'd' :return parseInt((dtEnd - dtStart) / 86400000);
case 'w' :return parseInt((dtEnd - dtStart) / (86400000 * 7));
case 'm' :return (dtEnd.getMonth() 1) ((dtEnd.getFullYear()-dtStart.getFullYear())*12) - (dtStart.getMonth() 1);
case 'y' :return dtEnd.getFullYear() - dtStart.getFullYear();
}
}

// ---------------------------------------------------
//| 日期输出字符串,重载了系统的toString方法
// ---------------------------------------------------
Date.prototype.toString = function(showWeek)
{
var myDate= this;
var str = myDate.toLocaleDateString();
if (showWeek)
{
var Week = ['日','一','二','三','四','五','六'];
str = ' 星期' Week[myDate.getDay()];
}
return str;
}

// ---------------------------------------------------
//| 日期合法性验证
//| 格式为:YYYY-MM-DD或YYYY/MM/DD
// ---------------------------------------------------
function IsValidDate(DateStr)
{
var sDate=DateStr.replace(/(^s |s $)/g,''); //去两边空格;
if(sDate=='') return true;
//如果格式满足YYYY-(/)MM-(/)DD或YYYY-(/)M-(/)DD或YYYY-(/)M-(/)D或YYYY-(/)MM-(/)D就替换为''
//数据库中,合法日期可以是:YYYY-MM/DD(2003-3/21),数据库会自动转换为YYYY-MM-DD格式
var s = sDate.replace(/[d]{ 4,4 }[-/]{ 1 }[d]{ 1,2 }[-/]{ 1 }[d]{ 1,2 }/g,'');
if (s=='') //说明格式满足YYYY-MM-DD或YYYY-M-DD或YYYY-M-D或YYYY-MM-D
{
var t=new Date(sDate.replace(/-/g,'/'));
var ar = sDate.split(/[-/:]/);
if(ar[0] != t.getYear() || ar[1] != t.getMonth() 1 || ar[2] != t.getDate())
{
//alert('错误的日期格式!格式为:YYYY-MM-DD或YYYY/MM/DD。注意闰年。');
return false;
}
}
else
{
//alert('错误的日期格式!格式为:YYYY-MM-DD或YYYY/MM/DD。注意闰年。');
return false;
}
return true;
}

// ------------------ --------------------------------
//| Date and time check
//| The format is: YYYY-MM-DD HH:MM:SS
// ---------------------------------- ----------------
function CheckDateTime(str)
{
var reg = /^(d )-(d{ 1,2 })-( d{ 1,2 }) (d{ 1,2 }):(d{ 1,2 }):(d{ 1,2 })$/;
var r = str.match(reg);
if(r==null)return false;
r[2]=r[2]-1;
var d= new Date(r[1],r[2],r[3] ,r[4],r[5],r[6]);
if(d.getFullYear()!=r[1])return false;
if(d.getMonth()!=r [2])return false;
if(d.getDate()!=r[3])return false;
if(d.getHours()!=r[4])return false;
if(d.getMinutes()!=r[5])return false;
if(d.getSeconds()!=r[6])return false;
return true;
}

//------------------------------------------------ ------
//| Split the date into arrays
// ---------------------------- -----------------------
Date.prototype.toArray = function()
{
var myDate = this;
var myArray = Array();
myArray[0] = myDate.getFullYear();
myArray[1] = myDate.getMonth();
myArray[2] = myDate.getDate();
myArray[3] = myDate.getHours();
myArray[4] = myDate.getMinutes();
myArray[5] = myDate.getSeconds();
return myArray;
}

// ------------------------------------------ ----------
//| Get date data information
//| The parameter interval represents the data type
//| y year m month d day w week ww week h hour n Minutes s seconds
//--------------------------------------------- ---------
Date.prototype.DatePart = function(interval)
{
var myDate = this;
var partStr='';
var Week = [ '日','一','二','三','四','五','六'];
switch (interval)
{
case 'y' :partStr = myDate.getFullYear();break;
case 'm' :partStr = myDate.getMonth() 1;break;
case 'd' :partStr = myDate.getDate();break;
case ' w' :partStr = Week[myDate.getDay()];break;
case 'ww' :partStr = myDate.WeekNumOfYear();break;
case 'h' :partStr = myDate.getHours(); break;
case 'n' :partStr = myDate.getMinutes();break;
case 's' :partStr = myDate.getSeconds();break;
}
return partStr;
}

// ---------------------------------------- ----------
//| Get the maximum number of days in the month of the current date
// -------------------- -------------------------------
Date.prototype.MaxDayOfDate = function()
{
var myDate = this;
var ary = myDate.toArray();
var date1 = (new Date(ary[0],ary[1] 1,1));
var date2 = date1. dateAdd(1,'m',1);
var result = dateDiff(date1.Format('yyyy-MM-dd'),date2.Format('yyyy-MM-dd'));
return result;
}

// ---------------------------------------- ---------------
//| Convert string to date type
//| Format MM/dd/YYYY MM-dd-YYYY YYYY/MM/dd YYYY -MM-dd
// ------------------------------------------ ----------
function StringToDate(DateStr)
{
var converted = Date.parse(DateStr);
var myDate = new Date(converted);
if (isNaN(myDate))
{
//var delimCahar = DateStr.indexOf('/')!=-1?'/':'-';
var arys= DateStr.split( '-');
myDate = new Date(arys[0],--arys[1],arys[2]);
}
return myDate;
}

Page verification code
Copy code The code is as follows:









Untitled Document
< ;/head>


body>

Statement
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
如何使用Go语言中的时间函数生成日程日历并生成短信提醒?如何使用Go语言中的时间函数生成日程日历并生成短信提醒?Jul 30, 2023 pm 03:49 PM

如何使用Go语言中的时间函数生成日程日历并生成短信提醒?在当今快节奏的生活中,人们经常需要一个有效的方式来管理和提醒自己的日程安排。使用Go语言中的时间函数可以很方便地生成日程日历,并利用短信提醒功能及时提醒用户。本文将介绍如何使用Go语言中的时间函数生成日程日历,并结合代码示例讲解如何生成短信提醒。首先,我们需要导入time包,该包提供了与时间相关的函数和

如何使用Go语言中的时间函数生成日历并输出到HTML文件?如何使用Go语言中的时间函数生成日历并输出到HTML文件?Jul 29, 2023 pm 06:46 PM

如何使用Go语言中的时间函数生成日历并输出到HTML文件?随着互联网的发展,许多传统工具和应用也逐渐迁移到了电子设备上。日历作为一个重要的时间管理工具,也不例外。利用Go语言中的时间函数,我们可以轻松地生成一个日历,并将其输出为HTML文件,方便我们在电脑或手机上查看和使用。要完成这个任务,我们首先需要了解Go语言的时间函数,它可以帮助我们处理日期和时间相关

PHP时间函数实例:时间的比较PHP时间函数实例:时间的比较Jun 20, 2023 pm 09:04 PM

在Web开发中,处理时间是非常常见的任务。PHP提供了许多内置函数来处理时间和日期,这些函数使得在PHP中处理时间和日期变得更加轻松和高效。在本篇文章中,我们将探讨PHP时间函数的一个实例,即如何比较两个时间。PHP比较时间的方法PHP提供了几个函数可以用来比较两个时间。下面是这些函数的简要介绍:strtotime()strtotime()函数

如何使用Go语言中的时间函数生成日程日历并生成微信和邮件提醒?如何使用Go语言中的时间函数生成日程日历并生成微信和邮件提醒?Jul 30, 2023 pm 08:21 PM

如何使用Go语言中的时间函数生成日程日历并生成微信和邮件提醒?在现代社会中,时间管理变得越来越重要。为了高效地处理我们的日程安排,使用日程日历工具是必不可少的。而在这个信息时代,微信和邮件成为人们最常用的交流方式。因此,能够自动将日程提醒发送到微信和邮件中,将会在一定程度上提升我们的生活效率。Go语言作为一种强大的后端开发语言,提供了许多处理时间和日期的函数

如何使用Go语言中的时间函数生成日程日历并生成邮件提醒?如何使用Go语言中的时间函数生成日程日历并生成邮件提醒?Aug 02, 2023 pm 02:21 PM

如何使用Go语言中的时间函数生成日程日历并生成邮件提醒?引言:在日常生活和工作中,我们经常会有各种各样的日程安排和事务提醒,例如重要会议、生日礼物购买、旅行安排等等。为了更好地管理和跟踪这些日程,我们可以使用Go语言中的时间函数来生成日程日历,并通过邮件进行提醒。本文将介绍如何使用Go语言编写代码来实现这一功能。一、生成日程日历在Go语言中,可以使用time

如何使用Go语言中的时间函数生成日程日历并生成微信提醒?如何使用Go语言中的时间函数生成日程日历并生成微信提醒?Jul 30, 2023 am 10:09 AM

如何使用Go语言中的时间函数生成日程日历并生成微信提醒?一、引言日程管理是现代生活中必不可少的一部分,通过合理规划时间和安排任务,可以提高工作和生活效率。而随着移动互联网的发展,人们越来越习惯使用智能手机进行日程的管理和提醒。本文将介绍如何使用Go语言中的时间函数生成日程日历,并通过微信提醒用户。二、Go语言中的时间函数Go语言提供了time包来处理时间相关

如何使用MySQL中的TIME函数获取当前时间如何使用MySQL中的TIME函数获取当前时间Jul 13, 2023 am 09:31 AM

如何使用MySQL中的TIME函数获取当前时间在开发应用程序时,经常需要获取当前时间或者仅仅只关心时间部分。MySQL中的TIME函数可以帮助我们轻松获取当前时间,它可以返回一个表示当前时间的值。本文将介绍如何使用MySQL中的TIME函数以及一些常见的用法。首先,让我们来了解一下TIME函数的语法:TIME()TIME函数不需要任何参数,直接使用即可。它将

如何使用Go语言中的时间函数生成日程日历并生成短信、邮件和微信提醒?如何使用Go语言中的时间函数生成日程日历并生成短信、邮件和微信提醒?Jul 29, 2023 pm 02:29 PM

如何使用Go语言中的时间函数生成日程日历并生成短信、邮件和微信提醒?时间管理对于我们每个人都非常重要。无论是个人生活,还是工作事务,都需要我们合理安排时间,以便高效地完成各项任务。在日常生活中,我们可以通过使用日程日历来帮助自己管理时间。而在技术领域,我们可以利用Go语言中的时间函数来实现日程日历,并通过短信、邮件和微信提醒的方式来提醒自己及时完成任务。本文

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

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.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),