Last active
August 29, 2015 14:06
-
-
Save dunn/31445795cb9ea39f477a to your computer and use it in GitHub Desktop.
turn the crappy date strings from google calendar xml into Moment objects
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var moment = require('moment-timezone'); | |
module.exports = function(input){ | |
var eventTimes = { | |
start: false, | |
end: false | |
}; | |
var a,b; | |
var splitMarker = input.indexOf('to'); | |
if (splitMarker > -1) { | |
a = input.slice(0,splitMarker - 1); | |
b = input.slice(splitMarker + 3); | |
// now see if it's like 'Fri Dec 5, 2014 4pm to 6pm PST' or 'Fri | |
// Oct 17, 2014 4pm to Fri Oct 17, 2014 6pm PDT' by looking at the | |
// first character of the 'b' string | |
if ( /[0-9]/.test(b.charAt(0)) ) { | |
var n = a.length; | |
// walk backwards through the 'a' string until we find a space | |
while (a.charAt(n) !== ' '){ | |
n--; | |
} | |
// then add the date information from 'a' to the 'b' string | |
b = a.slice(0,n+1) + b; | |
} | |
} | |
else { | |
a = input; | |
} | |
// we'll use this to cut off anything after the last 'am' or 'pm' | |
var timeZone = /(.*m)[^m]*/; | |
if (a) { | |
a = a.replace(timeZone, "$1"); | |
// http://momentjs.com/docs/#/parsing/string-format/ | |
// http://momentjs.com/timezone/docs/#/using-timezones/parsing-in-zone/ | |
a = moment.tz(a, "ddd MMM DD, YYYY ha", "America/Los_Angeles"); | |
} | |
if (b) { | |
b = b.replace(timeZone, "$1"); | |
b = moment.tz(b, "ddd MMM DD, YYYY ha", "America/Los_Angeles"); | |
} | |
eventTimes.start = a; | |
eventTimes.end = b; | |
return eventTimes; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var parser = function(){ | |
// the sort of date strings you get from google calendar xml | |
var tests = ['Wed Sep 24, 2014 10am to 10am PDT', | |
'Fri Dec 5, 2014 4pm to 6pm PST', | |
'Fri Oct 17, 2014 4pm to Fri Oct 17, 2014 6pm PDT', | |
'Thu Sep 11, 2014' | |
]; | |
var parse = require('./googleCalDateParser.js'); | |
var j = tests.length; | |
while (j--){ | |
console.log(parse(tests[j])); | |
} | |
}; | |
parser(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment