Skip to content

Commit 3fe0f9f

Browse files
committed
Use AM PM time representation
Closes #12
1 parent a164ae0 commit 3fe0f9f

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

prettycron.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,12 @@ if ((!moment || !later) && (typeof require !== 'undefined')) {
9797
// hour or minute, print them in HH:MM format
9898

9999
var hm = [];
100+
var hour = moment();
100101
for (var i=0; i < schedule['h'].length; i++) {
101102
for (var j=0; j < schedule['m'].length; j++) {
102-
hm.push(zeroPad(schedule['h'][i]) + ':' + zeroPad(schedule['m'][j]));
103+
hour.hour(schedule['h'][i]);
104+
hour.minute(schedule['m'][j]);
105+
hm.push(hour.format("hh:mm A"));
103106
}
104107
}
105108
if (hm.length < 2) {
@@ -173,15 +176,23 @@ if ((!moment || !later) && (typeof require !== 'undefined')) {
173176
*/
174177
var getNextDate = function(cronspec, sixth) {
175178
var schedule = later.parse.cron(cronspec, sixth);
176-
return later.schedule(schedule).next();
179+
return moment.utc(later.schedule(schedule).next()).format("ddd, MMM Do YYYY, h:mm:ss A");;
177180
};
178181

182+
/*
183+
* Given a cronspec, return the next run date as a moment
184+
* (useful for the getNext
185+
*/
186+
var getNextMoment = function(cronspec, sixth) {
187+
var schedule = later.parse.cron(cronspec, sixth);
188+
return moment.utc(later.schedule(schedule).next());
189+
};
179190
/*
180191
* Given a cronspec, return a friendly string for when it will next run.
181192
* (This is just a wrapper for later.js and moment.js)
182193
*/
183194
var getNext = function(cronspec, sixth) {
184-
return moment( getNextDate( cronspec, sixth ) ).calendar();
195+
return getNextMoment(cronspec, sixth).calendar();
185196
};
186197

187198
//----------------

test/homepage-examples.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,16 @@ suite('homepage examples', function() {
88
[
99
{ cron: '0 * * * *', readable: 'Every hour, on the hour' },
1010
{ cron: '30 * * * 1', readable: 'Every 30th minute past every hour on Mon' },
11-
{ cron: '15,45 9,21 * * *', readable: '09:15, 09:45, 21:15 and 21:45 every day' },
12-
{ cron: '18,19 7 5 * *', readable: '07:18 and 07:19 on the 5th of every month' },
11+
{ cron: '15,45 9,21 * * *', readable: '09:15 AM, 09:45 AM, 09:15 PM and 09:45 PM every day' },
12+
{ cron: '18,19 7 5 * *', readable: '07:18 AM and 07:19 AM on the 5th of every month' },
1313
{ cron: '* * 25 12 *', readable: 'Every minute on the 25th in Dec' },
1414
{ cron: '0 * 1,3 * *', readable: 'Every hour, on the hour on the 1 and 3rd of every month' },
15-
{ cron: '0 17 * 1,4,7,10 *', readable: '17:00 every day in Jan, Apr, Jul and Oct' },
15+
{ cron: '0 17 * 1,4,7,10 *', readable: '05:00 PM every day in Jan, Apr, Jul and Oct' },
1616
{ cron: '15 * * * 1,2', readable: 'Every 15th minute past every hour on Mon and Tue' },
1717
{ cron: '* 8,10,12,14,16,18,20 * * *', readable: 'Every minute of 8, 10, 12, 14, 16, 18 and 20th hour' },
18-
{ cron: '0 12 15,16 1 3', readable: '12:00 on the 15 and 16th and every Wed in Jan' },
18+
{ cron: '0 12 15,16 1 3', readable: '12:00 PM on the 15 and 16th and every Wed in Jan' },
1919
{ cron: '0 4,8,12,4 * * 4,5,6', readable: 'Every 0th minute past the 4, 8 and 12th hour on Thu, Fri and Sat' },
20-
{ cron: '0 2,16 1,8,15,22 * 1,2', readable: '02:00 and 16:00 on the 1, 8, 15 and 22nd of every month and every Mon and Tue' },
20+
{ cron: '0 2,16 1,8,15,22 * 1,2', readable: '02:00 AM and 04:00 PM on the 1, 8, 15 and 22nd of every month and every Mon and Tue' },
2121
{ cron: '15 3,8,10,12,14,16,18 16 * *', readable: 'Every 15th minute past the 3, 8, 10, 12, 14, 16 and 18th hour on the 16th of every month' },
2222
{ cron: '2 8,10,12,14,16,18 * 8 0,3', readable: 'Every 2nd minute past the 8, 10, 12, 14, 16 and 18th hour on Sun and Wed in Aug' },
2323
].forEach(function(item) {

0 commit comments

Comments
 (0)