日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

tinydate.js[v0.3] 新增了字符串格式化为日期对象的函数

發布時間:2023/12/18 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 tinydate.js[v0.3] 新增了字符串格式化为日期对象的函数 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

更新說明

  • 加入了String類型的擴展成員 convertToDate() 可以直接將 字符串格式的日期轉換為Date對象。
  • 加入了String類型的擴展成員 convertToTimeSpan() 可以將 字符串格式的日期轉換為TimeSpan對象。
  • 修復了日期格式化為字符串的format函數中的bug。
  • tinydate.js v0.3

    Date.prototype.format = function (fmt) {var o = {"M+": this.getMonth() + 1, //月份 "d+": this.getDate(), //日 "H+": this.getHours(), //小時 "m+": this.getMinutes(), //分 "s+": this.getSeconds(), //秒 "q+": Math.floor((this.getMonth() + 3) / 3), //季度 "f+": this.getMilliseconds() //毫秒 };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; } //當前完整時間 Date.$nowDate = new Date().format("yyyy-MM-dd HH:mm:ss.ffff"); //獲取自定義格式的當前時間 Date.$now = function (fmt) {return new Date().format(fmt); } //計算時間差 Date.diff = function (sDate, eDate) {if (eDate == undefined || eDate == null)eDate = new Date();var stime = sDate.getTime();var etime = eDate.getTime();var diffTime = etime - stime;var timeSpan = new TimeSpan(diffTime);return timeSpan; } //添加年 Date.prototype.addYear = function (number) {this.setFullYear(this.getFullYear() + number); } //添加月 Date.prototype.addMonth = function (number){this.setMonth(this.getMonth()+number); } //添加日 Date.prototype.addDate = function (number){this.setDate(this.getDate()+number); } //添加小時 Date.prototype.addHours = function (number){this.setHours(this.getHours()+number); } //添加分 Date.prototype.addMinutes = function (number){this.setMinutes(this.getMinutes()+number); } //添加秒 Date.prototype.addSeconds = function (number){this.setSeconds(this.getSeconds()+number); } //添加毫秒 Date.prototype.addMilliseconds = function (number){this.setMilliseconds(this.getMilliseconds()+number); }//獲得一年中第一天的日期 Date.prototype.getTheFirstDateOfTheYear = function (date) {var year, month=0, day=1;if (date == undefined || date == null) {year = this.getFullYear();}else {year = date.getFullYear();}return new Date(year, month, day); } //獲得一年中最后一天的日期 Date.prototype.getTheLastDateOfTheYear = function (date) {var year, month = 11, day = 31;if (date == undefined || date == null) {year = this.getFullYear();}else {year = date.getFullYear();}return new Date(year, month, day); } //格式化當前日期 為 時限對象 Date.prototype.timeSpan = function () {return new TimeSpan(this); } //時限對象 function TimeSpan() {var o = new Object();o.year = 0;//年o.month = 0;//月o.day = 0;//日o.hours = 0;//時o.minutes = 0;//分o.seconds = 0;//秒o.milliseconds = 0;//毫秒o.totalYear = 0.00;//從時間原點的年o.totalMonth = 0.00;//從時間原點到現在的月o.totalDay = 0.00;//從時間原點到現在的天o.totalHours = 0.00;//從時間原點到現在的小時o.totalMinutes = 0.00;//從時間原點到現在的分o.totalSeconds = 0.00;//從時間原點到現在的秒o.totalMilliseconds = 0.00;//從時間原點到現在的毫秒//初始化對象o.init = function (timestamp) {var odate = new Date(timestamp);o.year = odate.getFullYear();o.month = odate.getMonth() + 1;o.day = odate.getDate();o.hours = odate.getHours();o.minutes = odate.getMinutes();o.seconds = odate.getSeconds();o.milliseconds = odate.getMilliseconds();o.totalMilliseconds = timestamp;o.totalSeconds = (timestamp / 1000).toFixed(2);o.totalMinutes = (timestamp / 1000 / 60).toFixed(2);o.totalHours = (timestamp / 1000 / 60 / 60).toFixed(2);o.totalDay = (timestamp / 1000 / 60 / 60 / 24).toFixed(2);o.totalMonth = o.year * 12;o.totalYear = o.year;}//無參則返回空對象if (arguments.length == 0) {}else if (typeof (arguments[0]) === "string") {o.init(new Date(arguments[0]));}else if (typeof (arguments[0]) === "number") {o.init(arguments[0]);} else if (typeof(arguments[0]) === "object") {o.init(arguments[0]);}return o; }//字符串轉換為Date String.prototype.convertToDate = function () {return new Date(Date.parse(this.replace(/-/g, "/"))); }//字符串轉換為TimeSpan String.prototype.convertToTimeSpan = function () {return new TimeSpan(new Date(Date.parse(this.replace(/-/g, "/")))); }

    點此下載: tinydate_0.3.js


    轉載于:https://www.cnblogs.com/nozer1993/p/9198941.html

    總結

    以上是生活随笔為你收集整理的tinydate.js[v0.3] 新增了字符串格式化为日期对象的函数的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。