js實(shí)現(xiàn)的日期操作類DateTime函數(shù)代碼
方法注解: |
將指定的天數(shù)加到此實(shí)例的值上。 |
將指定的小時(shí)數(shù)加到此實(shí)例的值上。 |
將指定的分鐘數(shù)加到此實(shí)例的值上。 |
將指定的毫秒數(shù)加到此實(shí)例的值上。 |
將指定的月份數(shù)加到此實(shí)例的值上。 |
將指定的秒數(shù)加到此實(shí)例的值上。 |
將指定的年份數(shù)加到此實(shí)例的值上。 |
將此實(shí)例的值與指定的 Date 值相比較,并指示此實(shí)例是早于、等于還是晚于指定的 Date 值。 |
返回一個(gè)數(shù)值相同的新DateTime對象 |
返回一個(gè)值,該值指示此實(shí)例是否與指定的 DateTime 實(shí)例相等。 |
獲取此實(shí)例的日期部分。 |
獲取此實(shí)例所表示的日期為該月中的第幾天。 |
獲取此實(shí)例所表示的日期是星期幾。 |
獲取此實(shí)例所表示日期的小時(shí)部分。 |
獲取此實(shí)例所表示日期的分鐘部分。 |
獲取此實(shí)例所表示日期的毫秒部分。 |
獲取此實(shí)例所表示日期的月份部分。 |
獲取此實(shí)例的下個(gè)月一日的DateTime對象 |
獲取此實(shí)例的下一個(gè)周日的DateTime對象 |
獲取此實(shí)例的下一個(gè)周日的DateTime對象 |
獲取此實(shí)例所表示日期的秒部分。 |
返回此實(shí)例的Date值 |
獲取此實(shí)例所表示日期的年份部分。 |
指示此實(shí)例是否是DateTime對象 |
將當(dāng)前 DateTime 對象的值轉(zhuǎn)換為其等效的短日期字符串表示形式。 |
將當(dāng)前 DateTime 對象的值轉(zhuǎn)換為其等效的短時(shí)間字符串表示形式。 |
將當(dāng)前 DateTime 對象的值轉(zhuǎn)換為其等效的字符串表示形式。 |
驗(yàn)證Add系列的方法參數(shù)是否合法 |
繼承自Date的方法 |
比較 DateTime 的兩個(gè)實(shí)例,并返回它們相對值的指示。 |
返回指定年和月中的天數(shù)。 |
返回一個(gè)值,該值指示 DateTime 的兩個(gè)實(shí)例是否相等。 |
返回指定的年份是否為閏年的指示。 |
獲取一個(gè) DateTime 對象,該對象設(shè)置為此計(jì)算機(jī)上的當(dāng)前日期和時(shí)間,表示為本地時(shí)間。 |
將日期和時(shí)間的指定字符串表示形式轉(zhuǎn)換為其等效的 DateTime。 |
獲取當(dāng)前日期,其時(shí)間組成部分設(shè)置為 00:00:00。 |
//表示時(shí)間上的一刻,通常以日期和當(dāng)天的時(shí)間表示。
function DateTime(year, month, day, hour, min, sec, millisec){
var d = new Date();
if (year || year == 0){
d.setFullYear(year);
}
if (month || month == 0){
d.setMonth(month - 1);
}
if (day || day == 0){
d.setDate(day);
}
if (hour || hour == 0){
d.setHours(hour);
}
if (min || min == 0){
d.setMinutes(min);
}
if (sec || sec == 0){
d.setSeconds(sec);
}
if (millisec || millisec == 0){
d.setMilliseconds(millisec);
}
//將指定的天數(shù)加到此實(shí)例的值上。
this.AddDays = function(value){
if(!ValidateAddMethodParam(value)){
return null;
}
var result = this.Clone();
result.GetValue().setDate(result.GetDay() + value);
return result;
}
//將指定的小時(shí)數(shù)加到此實(shí)例的值上。
this.AddHours = function(value){
if(!ValidateAddMethodParam(value)){
return null;
}
var result = this.Clone();
result.GetValue().setHours(result.GetHour() + value);
return result;
}
//將指定的分鐘數(shù)加到此實(shí)例的值上。
this.AddMinutes = function(value){
if(!ValidateAddMethodParam(value)){
return null;
}
var result = this.Clone();
result.GetValue().setMinutes(result.GetMinute() + value);
return result;
}
//將指定的毫秒數(shù)加到此實(shí)例的值上。
this.AddMilliseconds = function(value){
if(!ValidateAddMethodParam(value)){
return null;
}
var result = this.Clone();
result.GetValue().setMilliseconds(result.GetMillisecond() + value);
return result;
}
//將指定的月份數(shù)加到此實(shí)例的值上。
this.AddMonths = function(value){
if(!ValidateAddMethodParam(value)){
return null;
}
var result = this.Clone();
result.GetValue().setMonth(result.GetValue().getMonth() + value);
return result;
}
//將指定的秒數(shù)加到此實(shí)例的值上。
this.AddSeconds = function(value){
if(!ValidateAddMethodParam(value)){
return null;
}
var result = this.Clone();
result.GetValue().setSeconds(result.GetSecond() + value);
return result;
}
//將指定的年份數(shù)加到此實(shí)例的值上。
this.AddYears = function(value){
if(!ValidateAddMethodParam(value)){
return null;
}
var result = this.Clone();
result.GetValue().setFullYear(result.GetYear() + value);
return result;
}
//將此實(shí)例的值與指定的 Date 值相比較,并指示此實(shí)例是早于、等于還是晚于指定的 Date 值。
this.CompareTo = function(other){
var internalTicks = other.getTime();
var num2 = d.getTime();
if (num2 > internalTicks)
{
return 1;
}
if (num2 < internalTicks)
{
return -1;
}
return 0;
}
//返回一個(gè)數(shù)值相同的新DateTime對象
this.Clone = function(){
return new DateTime(
this.GetYear()
,this.GetMonth()
,this.GetDay()
,this.GetHour()
,this.GetMinute()
,this.GetSecond()
,this.GetMillisecond());
}
//返回一個(gè)值,該值指示此實(shí)例是否與指定的 DateTime 實(shí)例相等。
this.Equals = function(other){
return this.CompareTo(other) == 0;
}
//獲取此實(shí)例的日期部分。
this.GetDate = function(){
var result = new DateTime(d.getFullYear(), d.getMonth(), d.getDate(), 0, 0, 0, 0);
return result ;
}
//獲取此實(shí)例所表示的日期為該月中的第幾天。
this.GetDay = function(){
return d.getDate();
}
//獲取此實(shí)例所表示的日期是星期幾。
this.GetDayOfWeek = function(){
return d.getDay();
}
//獲取此實(shí)例所表示日期的小時(shí)部分。
this.GetHour = function(){
return d.getHours();
}
//獲取此實(shí)例所表示日期的分鐘部分。
this.GetMinute = function(){
return d.getMinutes();
}
//獲取此實(shí)例所表示日期的毫秒部分。
this.GetMillisecond = function(){
return d.getMilliseconds();
}
//獲取此實(shí)例所表示日期的月份部分。
this.GetMonth = function(){
return d.getMonth() + 1;
}
//獲取此實(shí)例的下個(gè)月一日的DateTime對象
this.GetNextMonthFirstDay = function(){
var result = new DateTime(this.GetYear(), this.GetMonth(), 1, 0, 0, 0, 0);
result = result.AddMonths(1);
return result;
}
//獲取此實(shí)例的下一個(gè)周日的DateTime對象
this.GetNextWeekFirstDay = function(){
var result = this.GetDate();
return result.AddDays(7 - result.GetDayOfWeek());
}
//獲取此實(shí)例的下一個(gè)周日的DateTime對象
this.GetNextYearFirstDay = function(){
return new DateTime(this.GetYear() + 1, 1, 1, 0, 0, 0, 0);
}
//獲取此實(shí)例所表示日期的秒部分。
this.GetSecond = function(){
return d.getSeconds();
}
//返回此實(shí)例的Date值
this.GetValue = function(){
return d;
}
//獲取此實(shí)例所表示日期的年份部分。
this.GetYear = function(){
return d.getFullYear();
}
//指示此實(shí)例是否是DateTime對象
this.IsDateTime = function(){}
//將當(dāng)前 DateTime 對象的值轉(zhuǎn)換為其等效的短日期字符串表示形式。
this.ToShortDateString = function(){
var result = "";
result = d.getFullYear() + "-" + (d.getMonth() + 1) + "-" + d.getDate();
return result;
}
//將當(dāng)前 DateTime 對象的值轉(zhuǎn)換為其等效的短時(shí)間字符串表示形式。
this.ToShortTimeString = function(){
var result = "";
result = d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds();
return result;
}
//將當(dāng)前 DateTime 對象的值轉(zhuǎn)換為其等效的字符串表示形式。
this.ToString = function(format){
if(typeof(format) == "string"){
}
return this.ToShortDateString() + " " + this.ToShortTimeString();
}
//驗(yàn)證Add系列的方法參數(shù)是否合法
function ValidateAddMethodParam(param){
if(typeof(param) != "number"){
return false;
}
return true;
}
//繼承自Date的方法
this.getTime = function(){
return d.getTime();
}
}
//比較 DateTime 的兩個(gè)實(shí)例,并返回它們相對值的指示。
DateTime.Compare = function(d1, d2){
return d1.CompareTo(d2);
}
//返回指定年和月中的天數(shù)。
DateTime.DaysInMonth = function(year, month){
if ((month < 1) || (month > 12))
{
return "月份[" + month + "]超出范圍";
}
var numArray = DateTime.IsLeapYear(year) ? DateTime.DaysToMonth366 : DateTime.DaysToMonth365;
return (numArray[month] - numArray[month - 1]);
}
//返回一個(gè)值,該值指示 DateTime 的兩個(gè)實(shí)例是否相等。
DateTime.Equals = function(d1, d2){
return d1.CompareTo(d2) == 0;
}
//返回指定的年份是否為閏年的指示。
DateTime.IsLeapYear = function(year)
{
if ((year < 1) || (year > 0x270f))
{
return "年份[" + year + "]超出范圍";
}
if ((year % 4) != 0)
{
return false;
}
if ((year % 100) == 0)
{
return ((year % 400) == 0);
}
return true;
}
//獲取一個(gè) DateTime 對象,該對象設(shè)置為此計(jì)算機(jī)上的當(dāng)前日期和時(shí)間,表示為本地時(shí)間。
DateTime.Now = new DateTime();
//將日期和時(shí)間的指定字符串表示形式轉(zhuǎn)換為其等效的 DateTime。
DateTime.Parse = function(s){
var result = new DateTime();
var value = result.GetValue();
value.setHours(0,0,0,0);
var dateRex = /\b[1-2][0-9][0-9][0-9][-]\d{1,2}[-]\d{1,2}\b/i;
if(dateRex.test(s)){
var dateStr = s.match(dateRex)[0];
try{
var dateParts = dateStr.split("-");
var year = dateParts[0] - 0;
var month = dateParts[1] - 1;
var day = dateParts[2] - 0;
value.setFullYear(year,month,day);
}catch(ex){
return null;
}
var timeRex = /\b\d{1,2}[:]\d{1,2}[:]\d{1,2}\b/i;
if(timeRex.test(s)){
var timeStr = s.match(timeRex)[0];
try{
var timeParts = timeStr.split(":");
var hour = timeParts[0] - 0;
var min = timeParts[1] - 0;
var sec = timeParts[2] - 0;
value.setHours(hour,min,sec);
}catch(ex){
}
}
}else{
return null;
}
return result;
}
//獲取當(dāng)前日期,其時(shí)間組成部分設(shè)置為 00:00:00。
DateTime.Today = new DateTime(null, null, null, 0, 0, 0, 0);
//靜態(tài)字段
DateTime.DaysToMonth365 = [ 0, 0x1f, 0x3b, 90, 120, 0x97, 0xb5, 0xd4, 0xf3, 0x111, 0x130, 0x14e, 0x16d ];
DateTime.DaysToMonth366 = [ 0, 0x1f, 60, 0x5b, 0x79, 0x98, 0xb6, 0xd5, 0xf4, 0x112, 0x131, 0x14f, 0x16e ];
- Moment.js 不容錯(cuò)過的超棒Javascript日期處理類庫
- 用JavaScript將從數(shù)據(jù)庫中讀取出來的日期型格式化為想要的類型。
- 5個(gè)最佳的Javascript日期處理類庫分享
- javascript學(xué)習(xí)筆記(六) Date 日期類型
- 解決 JScript 中使用日期類型數(shù)據(jù)時(shí)出現(xiàn)類型錯(cuò)誤的問題
- javascript類型系統(tǒng)——日期Date對象全面了解
- javascript 封裝Date日期類實(shí)例詳解
- JavaScript日期類型的一些用法介紹
- js(jQuery)獲取時(shí)間的方法及常用時(shí)間類搜集
- JavaScript日期工具類DateUtils定義與用法示例
相關(guān)文章
微信小程序跨頁面數(shù)據(jù)傳遞事件響應(yīng)實(shí)現(xiàn)過程解析
這篇文章主要介紹了微信小程序跨頁面數(shù)據(jù)傳遞事件響應(yīng)實(shí)現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12JavaScript實(shí)現(xiàn)驗(yàn)證碼案例
這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)驗(yàn)證碼案例,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10css和js實(shí)現(xiàn)彈出登錄居中界面完整代碼
這篇文章主要介紹了css和js實(shí)現(xiàn)彈出登錄居中界面,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-11-11JavaScript使用atan2來繪制箭頭和曲線的實(shí)例
下面小編就為大家?guī)硪黄狫avaScript使用atan2來繪制箭頭和曲線的實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-09-09js實(shí)現(xiàn)的xml對象轉(zhuǎn)json功能示例
這篇文章主要介紹了js實(shí)現(xiàn)的xml對象轉(zhuǎn)json功能,結(jié)合實(shí)例形式分析了javascript轉(zhuǎn)換成xml所涉及的字符串、對象、數(shù)組、遍歷等操作技巧與使用方法,需要的朋友可以參考下2016-12-12js動(dòng)態(tài)設(shè)置select下拉菜單的默認(rèn)選中項(xiàng)實(shí)例
今天小編就為大家分享一篇js動(dòng)態(tài)設(shè)置select下拉菜單的默認(rèn)選中項(xiàng)實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-08-08H5如何實(shí)現(xiàn)喚起APP及調(diào)試bug解決
這篇文章主要為大家介紹了H5如何實(shí)現(xiàn)喚起APP及調(diào)試bug解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05