javascript对日期加减操作格式化

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

javascript对日期加减操作格式化
展开全文
Js代码
1.<span style="font-size: small;">/**
2.* js 对date加减
3.*/
4.
5.
6.
7.Date.prototype.Format = function(fmt) {
8.// author: meizz
9.var o = {
10."M+" : this.getMonth() + 1, // 月份
11."d+" : this.getDate(), // 日
12."h+" : this.getHours(), // 小时
13."m+" : this.getMinutes(), // 分
14."s+" : this.getSeconds(), // 秒
15."q+" : Math.floor((this.getMonth() + 3) / 3), // 季度
16."S" : this.getMilliseconds()
17.// 毫秒
18.};
19.if (/(y+)/.test(fmt))
20.fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").s ubstr(4
21.- RegExp.$1.length));
22.for (var k in o)
23.if (new RegExp("(" + k + ")").test(fmt))
24.fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1)
25.(o[k])
26.: (("00" + o[k]).substr(("" + o[k]).length)));
27.return fmt;
28.}
29.
30.Date.prototype.addDays = function(d) {
31.this.setDate(this.getDate() + d);
32.};
33.
34.Date.prototype.addWeeks = function(w) {
35.this.addDays(w * 7);
36.};
37.
38.Date.prototype.addMonths = function(m) {
39.var d = this.getDate();
40.this.setMonth(this.getMonth() + m);
41.
42.if (this.getDate() < d)
43.this.setDate(0);
44.};
45.
46.Date.prototype.addYears = function(y) {
47.var m = this.getMonth();
48.this.setFullYear(this.getFullYear() + y);
49.
50.if (m < this.getMonth()) {
51.this.setDate(0);
52.}
53.};
54.
55.//js格式化时间
56.Date.prototype.toDateString = function(formatStr) {
57.var date = this;
58.var timeValues = function() {
59.};
60.timeValues.prototype = {
61.year : function() {
62.if (formatStr.indexOf("yyyy") >= 0) {
63.return date.getYear();
64.} else {
65.return date.getYear().toString().substr(2);
66.}
67.},
68.elseTime : function(val, formatVal) {
69.return formatVal >= 0 ? (val < 10 ? "0" + val : val) : (val );
70.},
71.month : function() {
72.return this.elseTime(date.getMonth() + 1, formatStr.in dexOf("MM"));
73.},
74.day : function() {
75.return this.elseTime(date.getDate(), formatStr.indexOf( "dd"));
76.},
77.hour : function() {
78.return this.elseTime(date.getHours(), formatStr.indexO f("hh"));
79.},
80.minute : function() {
81.return this.elseTime(date.getMinutes(), formatStr.index Of("mm"));
82.},
83.second : function() {
84.return this.elseTime(date.getSeconds(), formatStr.inde xOf("ss"));
85.}
86.}
87.var tV = new timeValues();
88.var replaceStr = {
89.year : ["yyyy", "yy"],
90.month : ["MM", "M"],
91.day : ["dd", "d"],
92.hour : ["hh", "h"],
93.minute : ["mm", "m"],
94.second : ["ss", "s"]
95.};
96.for (var key in replaceStr) {
97.formatStr = formatStr.replace(replaceStr[key][0], eval(" tV." + key
98.+ "()"));
99.formatStr = formatStr.replace(replaceStr[key][1], eval(" tV." + key
100.+ "()"));
101.}
102.return formatStr;
103.}
104.
105.function formatStrDate(date) {
106.var str = date.toDateString("yyyy-MM-
dd hh:mm:ss");
107.if (str.indexOf("00:00:00") != -1) {
108.return str.replace("00:00:00", "10:00:00");
109.}
110.return str;
111.}
112.
113.var date = new Date();
114.date.addDays(-3);
(date.toDateString("yyyy-MM-dd hh:mm:ss"));
116.
117.</span>。

相关文档
最新文档