Basis of rails-driven app for local business
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

34 lines
1.1 KiB

  1. // American Format: 12/31/2000 5:00 pm
  2. // Thanks, Wes Hays
  3. Date.prototype.toFormattedString = function(include_time){
  4. str = Date.padded2(this.getMonth() + 1) + '/' +Date.padded2(this.getDate()) + '/' + this.getFullYear();
  5. if (include_time) { hour=this.getHours(); str += " " + this.getAMPMHour() + ":" + this.getPaddedMinutes() + " " + this.getAMPM() }
  6. return str;
  7. }
  8. Date.parseFormattedString = function (string) {
  9. // Test these with and without the time
  10. // 11/11/1111 12pm
  11. // 11/11/1111 1pm
  12. // 1/11/1111 10:10pm
  13. // 11/1/1111 01pm
  14. // 1/1/1111 01:11pm
  15. // 1/1/1111 1:11pm
  16. var regexp = "(([0-1]?[0-9])\/[0-3]?[0-9]\/[0-9]{4}) *([0-9]{1,2}(:[0-9]{2})? *(am|pm))?";
  17. var d = string.match(new RegExp(regexp, "i"));
  18. if (d==null) {
  19. return Date.parse(string); // Give javascript a chance to parse it.
  20. }
  21. mdy = d[1].split('/');
  22. hrs = 0;
  23. mts = 0;
  24. if(d[3] != null) {
  25. hrs = parseInt(d[3].split('')[0], 10);
  26. if(d[5].toLowerCase() == 'pm') { hrs += 12; } // Add 12 more to hrs
  27. mts = d[4].split(':')[1];
  28. }
  29. return new Date(mdy[2], parseInt(mdy[0], 10)-1, mdy[1], hrs, mts, 0);
  30. }