dsparling / datemaker

jquery.datamaker.js provides an easy way to create Javascript Date objects using simple time interval indicators. Use datamaker to create Data objects to use with jQuery cookie plugin.

datemaker / jquery.datemaker.js
100644 166 lines (158 sloc) 7.279 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/**
* jquery.datemaker.js: Date Maker plugin
*
* Copyright (c) 2008 Doug Sparling
* Dual licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
*
*/
 
/**
*
* Create a Date object using the given date format string.
*
* Date formats like Perl CGI::Cookie:
* Date formats like Rails ActiveSupport::CoreExtension::Numeric::Time
*
* @option String s number of seconds
* @option String m number of minutes
* @option String h number of hours
* @option String d number of days
* @option String M number of months
* @option String y number of years
* @option String s x.second.ago
* @option String s x.second.from_now
* @option String s x.seconds.ago
* @option String s x.seconds.from_now
* @option String s x.minute.ago
* @option String s x.minute.from_now
* @option String s x.minutes.ago
* @option String s x.minutes.from_now
* @option String s x.hour.ago
* @option String s x.hour.from_now
* @option String s x.hours.ago
* @option String s x.hours.from_now
* @option String s x.day.ago
* @option String s x.day.from_now
* @option String s x.days.ago
* @option String s x.days.from_now
* @option String s x.month.ago
* @option String s x.month.from_now
* @option String s x.months.ago
* @option String s x.months.from_now
* @option String s x.year.ago
* @option String s x.year.from_now
* @option String s x.years.ago
* @option String s x.years.from_now
* @option String midnight
* @option String now
*
* @example
* $.datemaker('+30s'); // 30 seconds from now
* $.datemaker('+5m'); // 5 minutes from now
* $.datemaker('+1h'); // 1 hour from now
* $.datemaker('-1d'); // 1 day ago
* $.datemaker('+10d'); // 10 days from now
* $.datemaker('+2M'); // 2 months from now
* $.datemaker('-6M'); // 6 months ago
* $.datemaker('+1y'); // 1 year from now
* $.datemaker('30.seconds.ago'); // 30 seconds ago
* $.datemaker('30.seconds.from_now'); // 30 seconds from now
* $.datemaker('5.minutes.ago'); // 5 minutes ago
* $.datemaker('5.minutes.from_now'); // 5 minutes from now
* $.datemaker('1.hour.ago'); // 1 hour ago
* $.datemaker('1.hour.from_now'); // 1 hour from now
* $.datemaker('10.days.ago'); // 10 days ago
* $.datemaker('10.days.from_now'); // 10 days from now
* $.datemaker('6.months.ago'); // 6 months ago
* $.datemaker('6.months.from_now'); // 6 months from now
* $.datemaker('1.year.ago'); // 1 year ago
* $.datemaker('1.year.from_now'); // 1 year from now
* $.datemaker('midnight'); // midnight
* $.datemaker('now'); // now
* @desc Examples...
* @example $.cookie('cookie_name', 'cookie_value', { expires: $.datemaker('+1h') });
* @desc Using with jQuery cookie plugin
*
* @param String s Date format identifier.
* @return Date object
* @type Date object
*
* @name $.datemaker
* @cat Plugins/Date
* @author Doug Sparling/doug.sparling@gmail.com
* @version 1.0.0
*/
(function($) {
 
  $.datemaker = function(expires) {
 
    var d1 = new Date();
 
    if ( ar = expires.match(/^([+|-])?(\d+)(\w)$/) ) {
 
      var action;
      RegExp.$1 == '+' || RegExp.$1 == '' ? action = '+' : action = '-';
 
      if (RegExp.$3 == 's') {
        action == '-' ? $.datemaker.advanceSeconds(d1, '-', RegExp.$2) : $.datemaker.advanceSeconds(d1, '+', RegExp.$2);
      } else if (RegExp.$3 == 'm') {
        action == '-' ? $.datemaker.advanceMinutes(d1, '-', RegExp.$2) : $.datemaker.advanceMinutes(d1, '+', RegExp.$2);
      } else if (RegExp.$3 == 'h') {
        action == '-' ? $.datemaker.advanceHours(d1, '-', RegExp.$2) : $.datemaker.advanceHours(d1, '+', RegExp.$2);
      } else if (RegExp.$3 == 'd') {
        action == '-' ? $.datemaker.advanceDays(d1, '-', RegExp.$2) : $.datemaker.advanceDays(d1, '+', RegExp.$2);
      } else if (RegExp.$3 == 'M') {
        action == '-' ? $.datemaker.advanceMonths(d1, '-', RegExp.$2) : $.datemaker.advanceMonths(d1, '+', RegExp.$2);
      } else if (RegExp.$3 == 'y') {
        action == '-' ? $.datemaker.advanceYears(d1, '-', RegExp.$2) : $.datemaker.advanceYears(d1, '+', RegExp.$2);
      }
    } else if ( ar = expires.match(/^(\d+)\.seconds?\.ago$/) ) {
      $.datemaker.advanceSeconds(d1, '-', RegExp.$1);
    } else if ( ar = expires.match(/^(\d+)\.seconds?\.from_now$/) ) {
      $.datemaker.advanceSeconds(d1, '+', RegExp.$1);
    } else if ( ar = expires.match(/^(\d+)\.minutes?\.ago$/) ) {
      $.datemaker.advanceMinutes(d1, '-', RegExp.$1);
    } else if ( ar = expires.match(/^(\d+)\.minutes?\.from_now$/) ) {
      $.datemaker.advanceMinutes(d1, '+', RegExp.$1);
    } else if ( ar = expires.match(/^(\d+)\.hours?\.ago$/) ) {
      $.datemaker.advanceHours(d1, '-', RegExp.$1);
    } else if ( ar = expires.match(/^(\d+)\.hours?\.from_now$/) ) {
      $.datemaker.advanceHours(d1, '+', RegExp.$1);
    } else if ( ar = expires.match(/^(\d+)\.days?\.ago$/) ) {
      $.datemaker.advanceDays(d1, '-', RegExp.$1);
    } else if ( ar = expires.match(/^(\d+)\.days?\.from_now$/) ) {
      $.datemaker.advanceDays(d1, '+', RegExp.$1);
    } else if ( ar = expires.match(/^(\d+)\.months?\.ago$/) ) {
      $.datemaker.advanceMonths(d1, '-', RegExp.$1);
    } else if ( ar = expires.match(/^(\d+)\.months?\.from_now$/) ) {
      $.datemaker.advanceMonths(d1, '+', RegExp.$1);
    } else if ( ar = expires.match(/^(\d+)\.years?\.ago$/) ) {
      $.datemaker.advanceYears(d1, '-', RegExp.$1);
    } else if ( ar = expires.match(/^(\d+)\.years?\.from_now$/) ) {
      $.datemaker.advanceYears(d1, '+', RegExp.$1);
    } else if (ar = expires.match(/^now$/)) {
      // just return d1
    } else if (ar = expires.match(/^midnight$/)) {
       d1.setDate(d1.getDate()+1);
       d1.setHours(0);
       d1.setMinutes(0);
       d1.setSeconds(0);
    }
    return d1;
  };
 
  // Advance functions
  $.datemaker.advanceSeconds = function(d1, direction, seconds) {
      direction == '-' ? d1.setSeconds( d1.getSeconds() - parseInt(seconds) ) : d1.setSeconds( d1.getSeconds() + parseInt(seconds) );
  }
  $.datemaker.advanceMinutes = function(d1, direction, minutes) {
      direction == '-' ? d1.setMinutes( d1.getMinutes() - parseInt(minutes) ) : d1.setMinutes( d1.getMinutes() + parseInt(minutes) );
  }
  $.datemaker.advanceHours = function(d1, direction, hours) {
      direction == '-' ? d1.setHours( d1.getHours() - parseInt(hours) ) : d1.setHours( d1.getHours() + parseInt(hours) );
  }
  $.datemaker.advanceDays = function(d1, direction, days) {
      direction == '-' ? d1.setDate( d1.getDate() - parseInt(days) ) : d1.setDate( d1.getDate() + parseInt(days) );
  }
  $.datemaker.advanceMonths = function(d1, direction, months) {
      direction == '-' ? d1.setMonth( d1.getMonth() - parseInt(months) ) : d1.setMonth( d1.getMonth() + parseInt(months) );
  }
  $.datemaker.advanceYears = function(d1, direction, years) {
      direction == '-' ? d1.setFullYear( d1.getFullYear() - parseInt(years) ) : d1.setFullYear( d1.getFullYear() + parseInt(years) );
  };
}) (jQuery)