0

Assuming that time formats (not timezone data, just plain-text Strings representing time in 12-hour clock), what is the best way to parse that data (examples below)?

I feel like this is a pretty simple question, but I've though about it a lot and can't figure out a fool-proof process for parsing out hour and minute values from any one of many time "formats".

I'm using a simple form that allows for entering a time (e.g., the time of a reservation), but there is no way to make the form field (Google Forms, limited customizability) anything other than plain-text which allows for any time format a person wishes to put in there a viable piece of data to work with. Here are some examples of times that can be entered:

1 P        // hours would be 13, minutes would be 0
3:15 P.M.  // hours would be 15, minutes would be 15
4 P.M      // hours would be 16, minutes would be 0
8:30 a     // hours would be 8, minutes would be 30
10:45 aM.  // hours would be 10, minutes would be 45
12:00 Pm   // hours would be 12, minutes would be 0

Those are just some examples. Feel free to fix tags if I'm not in the right topic(s) - I am however using Google Apps Script/Javascript to accomplish this.

I thought about building a couple regex strings to look for certain formats (which I'm sure will take me a while to write since I've never written one before).

I also thought about looking for a ':' character, and an 'a'/'A' character or 'p'/'P' for determing whether or not the +12 hours needs to be added. After that kind of information is retrieved the problem could be solved by truncating the 'p.m.'-type sections, then split(":") to get the correct numbers.

I feel like the first approach is much more stable, but the second solution might be easier. I'm not sure if there is a better way though, those are the two methods my n00b programmer mind could come up with.

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
Chris Cirefice
  • 5,475
  • 7
  • 45
  • 75
  • don't try sniffing hours and other specifics from dates, parse the whole thing using a date library, recommendations to follow i'm sure. – dandavis Jun 24 '13 at 15:48
  • You're not restricted to text input; why not use a date/time picker? http://stackoverflow.com/a/14393878/1677912 – Mogsdad Jun 24 '13 at 15:50
  • @dandavis I completely forgot that I can include another person's library in my GAS project, I'll take a look right now to see if I can find any date/time libraries. However, I'm almost certain that I can't arbitrarily use any Javascript library using Google's service. And Mogsdad, I haven't gotten around to implementing a GUI component for this yet. As a matter of fact, my boss said he would like to keep everything internalized (embedded), and not redirect to a Google Site or anything of the like (don't ask me why haha). Maybe in a month he'll come around, until then, just Forms and GAS. – Chris Cirefice Jun 24 '13 at 15:53
  • if you can run some script, you can run all script, unless it's using some restricted js subset like FBJS, caja, adsafe, etc. Many date libs are small, so you can likely simply paste the lib code above your code and it will function properly. – dandavis Jun 24 '13 at 15:57
  • It's also a shame that SO didn't redirect me to [this question](http://stackoverflow.com/questions/141348/what-is-the-best-way-to-parse-a-time-into-a-date-object-from-user-input-in-javas?rq=1), because he is having the same issue that I am - and the solution was regex parsing (but I'm not too familiar with that so it might be a bad idea). Is there any solution aside from building a GUI and putting all the form content hard-coded into a Google Site? Right now all I can work with is Google Forms, Spreadsheets and GAS =/ – Chris Cirefice Jun 24 '13 at 15:57
  • Alright @dandavis, you make a good point. I'll take a look at some libraries and see what I can find! – Chris Cirefice Jun 24 '13 at 15:58
  • if it's a google spreadsheet form, you can make your own form last i tried (~4 months). If you spoof the input names and values and submit to the same url as the google-generated form, they don't seem to care about referrer, so the external submit WILL add a new row to your spreadsheet... – dandavis Jun 24 '13 at 16:02
  • Oh wow, that definitely sounds like it would work, but I think building a Google Site would be the first alternative as their interface is pretty simple. So update - I looked through GAS libraries and couldn't find anything to format times. There were a lot of libraries for dates, but nothing for time that did what I was hoping. Anyway, back to the question - aside from creating a Google Site and using the date/time picker UI tool, is there a way that I can parse out the `hour` and `miunte` from those time formats? I'm unfamiliar with regex but I'm willing to try it out if that's best. – Chris Cirefice Jun 24 '13 at 16:14
  • Alright, looked into the whole library idea and found something really really cool which I definitely think does **exactly** what I need it to: [Datejs](http://www.datejs.com/). So I'll be working with that, thanks for the help guys, I wished SO would have directed me to the link I mentioned in one of my previous comments (because I posted a duplicate, and worse one that was already answered). So I'll be deleting this post tomorrow! Thanks again :) – Chris Cirefice Jun 24 '13 at 16:55

1 Answers1

1

I'm glad to hear you have an answer that you think will help you out. But before you write too much code, revisit this assertion you made:

...there is no way to make the form field (Google Forms, limited customizability) anything other than plain-text

Here's how you define a question to present a Date & Time picker, using the Forms UI.

Screen shot: form date/time picker

The spreadsheet that is receiving your form responses will end up with a Date object - no need to mess around with strings at all.

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
  • HOLY FRICK. This was not a feature literally 2 weeks ago when I finalized my forms. You have got to be kidding me... now I'm actually kind of annoyed, because I've been WAITING for this for so long... How do I subscribe myself to a newsletter that tells me when Google adds features to their products? Because this would have saved me 6 hours of Googling, asking a useless duplicate question on S.O. and writing 300 lines of complicated time-manipulation code to make sure everything came into the Spreadsheets correctly. Once again Mogsdad, you're the man! – Chris Cirefice Jun 26 '13 at 14:14
  • @ChrisCirefice - It's an ever-evolving world, that's for sure. I saw an [unofficial announcement](https://productforums.google.com/d/msg/docs/NHoSXSVc0IY/ZeOfT7MTr8QJ) of this mid-May, so it's pretty new. All I can suggest is coming here or going to that forum earlier in the process, before you commit too much time. With apps-script, the public issue-tracker gives you a way to get notified about requests you care about, but the forum for Docs/Drive isn't as helpful. – Mogsdad Jun 26 '13 at 14:34
  • Alright, I'll try to get myself more involved with the issue-tracker. I'm actually really glad I posted this question when I did - the lack of a date/time picker in the Google Forms UI was about to drive me to create a whole Google Site just to host code-generated forms that could have a date/time UI. I love Google, but it would be really nice if they had an obvious way to subscribe to product newsletters. I've looked around, and I don't see any. Unfortunate, but that's the way it is I suppose! Anyway thanks a ton for pointing this feature out to me, saved me a giant headache :) – Chris Cirefice Jun 26 '13 at 15:26
  • this blog is also a good way to keep informed :http://googleappsdeveloper.blogspot.be/ – Serge insas Jul 17 '13 at 20:44