更新说明

  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;
}//字符串转换为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] 新增了字符串格式化为日期对象的函数相关推荐

  1. Python语言学习:基于python五种方法实现使用某函数名【func_01】的字符串格式('func_01')来调用该函数【func_01】执行功能

    Python语言学习:基于python五种方法实现使用某函数名[func_01]的字符串格式('func_01')来调用该函数[func_01]执行功能 目录 问题提出 代码五种设计思路 T1方法:e ...

  2. 将数字字符串格式化为指定长度

    将数字字符串格式化为指定长度的JavaScript函数 function formatNum(number,len){var strLength = len - number.length;//格式长 ...

  3. jsp中两个字符串格式的日期可以相减吗_举个栗子!Tableau 技巧(126):学几个常用的日期函数...

    当我们的数据源有日期字段时,想根据日期实现需要的分析呈现,使用函数对日期进行计算,就非常有必要了! Tableau 中的日期函数很多,今天我们分享数据粉常用的 7 个函数,看它们如何帮助实现日期的计算 ...

  4. java中如何把时间封装成类,java-如何在不使用任何不推荐使用的类的情况下将日期从一种格式转换为另一种格式的日期对象?...

    java-如何在不使用任何不推荐使用的类的情况下将日期从一种格式转换为另一种格式的日期对象? 我想将date1格式的日期转换为date2格式的日期对象. SimpleDateFormat simple ...

  5. JAVA将十位时间戳格式化为日期Date类型

    一.十位数字格式时间戳格式化为日期Date类型 public static String timeStamp2Date(String seconds,String format) { if(secon ...

  6. JS 日期对象常用函数(时间常用函数、日期格式化)

    Date 对象属性 属性 描述 constructor 返回对创建此对象的 Date 函数的引用. prototype 使您有能力向对象添加属性和方法. Date 对象方法 方法 描述 getDate ...

  7. fastjson将json字符串转化为java对象

    目录 一.导入一个fastjson的jar包 二.Json字符串格式 三.根据json的格式创建Java类 四.给java类的所有属性添加setter方法 五.转换为java对象 一.导入一个fast ...

  8. sscanf,sscanf_s及其相关用法(字符串格式化为其他类型)

    #include<stdio.h> 定义函数 int sscanf (const char *str,const char * format,........); 函数说明   sscan ...

  9. 自动生成 指定范围日期 生成字符串格式时间日期 --计算连续时间 SQL

    DECLARE @StartTime AS DATETIME SET @StartTime= '2016-5-10 ' DECLARE @t TABLE(dDate DATETIME) --计算连续时 ...

最新文章

  1. jquery rotate.js
  2. 别再问Cloudflare CDN 漏洞是怎么被利用的啦!这篇文就来告诉你
  3. squid mysql认证_Squid 3.1.7通过mysql_auth方式认证
  4. 【空间数据库】ArcSDE 10.7+SQLEXPRESS+ArcServer 10.7.ecp企业级数据库环境搭建
  5. opencv:Gamma校正
  6. 我的k8s随笔:命令实践
  7. linux内核实时调度,基于Linux内核的实时调度机制的研究和实现
  8. python创建函数如何调用字典对象_我不知道如何用Python创建一个调用我函数的字典...
  9. BooKu手机电子书阅读器,正式推出了
  10. OpenGL with QtWidgets:材质、光照贴图
  11. vss服务器状态失败_Exchange快照错误,询问写入器状态失败
  12. 什么是阿里云服务器ECS?
  13. 被野性消费的黄桃罐头,还能火多久!
  14. 中国传统文化-雨课堂期末2022
  15. 初学solidworks,这些基础知识你必须要掌握!
  16. iOS-如何开发一款类 Runkeeper 的跑步应用 (上)
  17. STL——STL简介、STL六大组件
  18. weblogic8.1 下载地址
  19. Linux常用命令——ssh命令
  20. jenkins 使用过程问题解决方法

热门文章

  1. 【LeetCode-SQL每日一题】——183. 从不订购的客户
  2. 【机器学习】 - keras学习 - 图片生成器ImageDataGenerator
  3. ACM 题目分类POJ(自用,精)
  4. 【51Nod - 1416】两点 (dfs 或 并查集+dfs)
  5. 【CodeForces - 1051B】Relatively Prime Pairs (构造,思维,素数,水题)
  6. centos7卸载mysql数据库,CentOS7 安装卸载MySQL
  7. IO流配置文件,键值对Properties 的读取
  8. 评价微型计算机总线的性能指标,计算机组成原理学习笔记(3):总线
  9. 替换string中的部分字符
  10. redis——持久化