博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
tinydate.js[v0.3] 新增了字符串格式化为日期对象的函数
阅读量:4641 次
发布时间:2019-06-09

本文共 4499 字,大约阅读时间需要 14 分钟。

更新说明

  1. 加入了String类型的扩展成员 convertToDate() 可以直接将 字符串格式的日期转换为Date对象。
  2. 加入了String类型的扩展成员 convertToTimeSpan() 可以将 字符串格式的日期转换为TimeSpan对象。
  3. 修复了日期格式化为字符串的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;}//字符串转换为DateString.prototype.convertToDate = function () {    return new Date(Date.parse(this.replace(/-/g, "/")));}//字符串转换为TimeSpanString.prototype.convertToTimeSpan = function () {    return new TimeSpan(new Date(Date.parse(this.replace(/-/g, "/"))));}

点此下载:


转载于:https://www.cnblogs.com/nozer1993/p/9198941.html

你可能感兴趣的文章
head/tail实现
查看>>
sql语句的各种模糊查询语句
查看>>
vlc 学习网
查看>>
Python20-Day05
查看>>
Real World Haskell 第七章 I/O
查看>>
C#操作OFFICE一(EXCEL)
查看>>
【js操作url参数】获取指定url参数值、取指定url参数并转为json对象
查看>>
ABAP 程序间的调用
查看>>
移动端单屏解决方案
查看>>
web渗透测试基本步骤
查看>>
使用Struts2标签遍历集合
查看>>
angular.isUndefined()
查看>>
第一次软件工程作业(改进版)
查看>>
网络流24题-飞行员配对方案问题
查看>>
Jenkins 2.16.3默认没有Launch agent via Java Web Start,如何配置使用
查看>>
引入css的四种方式
查看>>
iOS开发UI篇—transframe属性(形变)
查看>>
3月7日 ArrayList集合
查看>>
jsp 环境配置记录
查看>>
Python03
查看>>