SoFunction
Updated on 2025-03-10

Js Get the current date and time and other operation implementation code

In JavaScript, methods in the Date object can be used to get the current date. Use var myDate=new Date() to create a date object, and then use the toLocaleDateString() method to get the current date.

Date objects are used to process dates and times.

Syntax for creating a Date object:

var myDate=new Date()

Note: The Date object will automatically save the current date and time as its initial value.

The method of obtaining the date object is as follows:

(); //Get the current year (2 digits)
(); //Get the full year (4 digits, 1970-?????)
(); //Get the current month (0-11, 0 represents January)
(); //Get the current day (1-31)
(); //Get the current week X (0-6, 0 represents Sunday)
(); //Get the current time (number of milliseconds starting from 1970.1.1)
(); //Get the current number of hours (0-23)
(); //Get the current number of minutes (0-59)
(); //Get the current number of seconds (0-59)
(); //Get the current number of milliseconds (0-999)
(); //Get the current date
var mytime=(); //Get the current time
( ); //Get date and time

Date and time script library method list

Judge leap year
Date formatting
Date calculation
Compare date difference
Date to string
Date split into array
Take some information about the date
Take the maximum number of days in the month where the date is
What week of the year
StringToDate String to Date
IsValidDate Verify date validity
CheckDateTime Full date and time check
daysBetween date day difference

//--------------------------------------------------- 
// Judge leap year//--------------------------------------------------- 
 = function() 
{ 
return (0==()%4&&((()%100!=0)||(()%400==0))); 
} 
//--------------------------------------------------- 
// Date formatting// Format YYYY/yyy/YY/yy indicates year// MM/M month// W/w Week// dd/DD/d/D Date// hh/HH/h/H Time// mm/m min// ss/SS/s/S seconds//--------------------------------------------------- 
 = function(formatStr) 
{ 
var str = formatStr; 
var Week = ['day','one','two','three','Four','five','six']; 
str=(/yyyy|YYYY/,()); 
str=(/yy|YY/,(() % 100)>9?(() % 100).toString():'0' + (() % 100)); 
str=(/MM/,()>9?().toString():'0' + ()); 
str=(/M/g,()); 
str=(/w|W/g,Week[()]); 
str=(/dd|DD/,()>9?().toString():'0' + ()); 
str=(/d|D/g,()); 
str=(/hh|HH/,()>9?().toString():'0' + ()); 
str=(/h|H/g,()); 
str=(/mm/,()>9?().toString():'0' + ()); 
str=(/m/g,()); 
str=(/ss|SS/,()>9?().toString():'0' + ()); 
str=(/s|S/g,()); 
return str; 
} 
//+--------------------------------------------------- 
//| Find the difference in the number of days between two times. The date format is YYYY-MM-dd//+--------------------------------------------------- 
function daysBetween(DateOne,DateTwo) 
{ 
var OneMonth = (5, ('-')); 
var OneDay = (, ('-')+1); 
var OneYear = (0, ('-')); 
var TwoMonth = (5, ('-')); 
var TwoDay = (, ('-')+1); 
var TwoYear = (0, ('-')); 
var cha=(((OneMonth+'/'+OneDay+'/'+OneYear)- (TwoMonth+'/'+TwoDay+'/'+TwoYear))/86400000); 
return (cha); 
} 
//+--------------------------------------------------- 
//| Date calculation//+--------------------------------------------------- 
 = function(strInterval, Number) { 
var dtTmp = this; 
switch (strInterval) { 
case 's' :return new Date((dtTmp) + (1000 * Number)); 
case 'n' :return new Date((dtTmp) + (60000 * Number)); 
case 'h' :return new Date((dtTmp) + (3600000 * Number)); 
case 'd' :return new Date((dtTmp) + (86400000 * Number)); 
case 'w' :return new Date((dtTmp) + ((86400000 * 7) * Number)); 
case 'q' :return new Date((), (()) + Number*3, (), (), (), ()); 
case 'm' :return new Date((), (()) + Number, (), (), (), ()); 
case 'y' :return new Date((() + Number), (), (), (), (), ()); 
} 
} 
//+--------------------------------------------------- 
//| Comparison date difference dtEnd format is date type or valid date format string//+--------------------------------------------------- 
 = function(strInterval, dtEnd) { 
var dtStart = this; 
if (typeof dtEnd == 'string' )// If it is a string converted to a date type{ 
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 (()+1)+((()-())*12) - (()+1); 
case 'y' :return () - (); 
} 
} 
//+--------------------------------------------------- 
//| Date output string, overloading the system's toString method//+--------------------------------------------------- 
 = function(showWeek) 
{ 
var myDate= this; 
var str = (); 
if (showWeek) 
{ 
var Week = ['day','one','two','three','Four','five','six']; 
str += ' Week' + Week[()]; 
} 
return str; 
} 
//+--------------------------------------------------- 
//| Verification of date legality//| Format: YYYY-MM-DD or YYYY/MM/DD//+--------------------------------------------------- 
function IsValidDate(DateStr) 
{ 
var sDate=(/(^\s+|\s+$)/g,''); //Get the spaces on both sides;if(sDate=='') return true; 
//If the format satisfies YYYY-(/)MM-(/)DD or YYYY-(/)M-(/)DD or YYYY-(/)M-(/)D or YYYY-(/)MM-(/)D, it will be replaced with ''//In the database, the legal date can be: YYYY-MM/DD (2003-3/21), and the database will be automatically converted to YYYY-MM-DD formatvar s = (/[\d]{ 4,4 }[\-/]{ 1 }[\d]{ 1,2 }[\-/]{ 1 }[\d]{ 1,2 }/g,''); 
if (s=='') //The format of the explanation satisfies YYY-MM-DD or YYYY-M-DD or YYYY-M-D or YYYY-MM-D or YYYY-MM-D{ 
var t=new Date((/\-/g,'/')); 
var ar = (/[-/:]/); 
if(ar[0] != () || ar[1] != ()+1 || ar[2] != ()) 
{ 
//alert('Wrong date format! Format is: YYYY-MM-DD or YYYY/MM/DD. Pay attention to leap years.');return false; 
} 
} 
else 
{ 
//alert('Wrong date format! Format is: YYYY-MM-DD or YYYY/MM/DD. Pay attention to leap years.');return false; 
} 
return true; 
} 
//+--------------------------------------------------- 
//| Date and time check//| Format: 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 = (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(()!=r[1])return false; 
if(()!=r[2])return false; 
if(()!=r[3])return false; 
if(()!=r[4])return false; 
if(()!=r[5])return false; 
if(()!=r[6])return false; 
return true; 
} 
//+--------------------------------------------------- 
//| Split dates into arrays//+--------------------------------------------------- 
 = function() 
{ 
var myDate = this; 
var myArray = Array(); 
myArray[0] = (); 
myArray[1] = (); 
myArray[2] = (); 
myArray[3] = (); 
myArray[4] = (); 
myArray[5] = (); 
return myArray; 
} 
//+--------------------------------------------------- 
//| Obtain date data information//| Parameter interval represents the data type//| y year m month d day w week ww week h hour n minute s seconds//+--------------------------------------------------- 
 = function(interval) 
{ 
var myDate = this; 
var partStr=''; 
var Week = ['day','one','two','three','Four','five','six']; 
switch (interval) 
{ 
case 'y' :partStr = ();break; 
case 'm' :partStr = ()+1;break; 
case 'd' :partStr = ();break; 
case 'w' :partStr = Week[()];break; 
case 'ww' :partStr = ();break; 
case 'h' :partStr = ();break; 
case 'n' :partStr = ();break; 
case 's' :partStr = ();break; 
} 
return partStr; 
} 
//+--------------------------------------------------- 
//| Get the maximum number of days in the month when the current date is located//+--------------------------------------------------- 
 = function() 
{ 
var myDate = this; 
var ary = (); 
var date1 = (new Date(ary[0],ary[1]+1,1)); 
var date2 = (1,'m',1); 
var result = dateDiff(('yyyy-MM-dd'),('yyyy-MM-dd')); 
return result; 
} 
//+--------------------------------------------------- 
//| What week is the current date in which week is the year//+--------------------------------------------------- 
 = function() 
{ 
var myDate = this; 
var ary = (); 
var year = ary[0]; 
var month = ary[1]+1; 
var day = ary[2]; 
('< script language=VBScript\> \n'); 
('myDate = DateValue(''+month+'-'+day+'-'+year+'') \n'); 
('result = DatePart('ww', myDate) \n'); 
(' \n'); 
return result; 
} 
//+--------------------------------------------------- 
//| Convert string to date type//| Format MM/dd/YYYY MM-dd-YYYY YYY/MM/dd YYYY-MM-dd//+--------------------------------------------------- 
function StringToDate(DateStr) 
{ 
var converted = (DateStr); 
var myDate = new Date(converted); 
if (isNaN(myDate)) 
{ 
//var delimCahar = ('/')!=-1?'/':'-'; 
var arys= ('-'); 
myDate = new Date(arys[0],--arys[1],arys[2]); 
} 
return myDate; 
}

This is the article about Js getting the current date, time and other operation implementation code. For more related Js, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!