• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright 2009 Google Inc. All Rights Reserved.
2
3/**
4 * @fileoverview Utility functions for formating date.
5 */
6
7/**
8 * Formats start date and end date in JSON format to string of format:
9   "09/09/2010 20:00 PM to 22:00 PM PST"
10 * @param {object} start date in JSON format.
11 * @param {object} end date in JSON format.
12 * @param {string} formatted date string.
13 */
14function formatDate(start,end) {
15  var s_ampm = null;
16  var e_ampm = null;
17
18  var toStr = function (num) {
19    if (num <= 12) {
20      return "" + num;
21    } else {
22      return "" + (num - 12);
23    }
24  };
25
26  var getMonthName = function (num) {
27    switch(num) {
28      case 1:
29        return 'January';
30      case 2:
31        return 'February';
32      case 3:
33        return 'March';
34      case 4:
35        return 'April';
36      case 5:
37        return 'May';
38      case 6:
39        return 'June';
40      case 7:
41        return 'July';
42      case 8:
43        return 'August';
44      case 9:
45        return 'September';
46      case 10:
47        return 'October';
48      case 11:
49        return 'November';
50      case 12:
51        return 'December';
52     }
53  }
54
55  var regex =  /(^\d{4})-(\d{2})-(\d{2})\s{1}(\d{2}):(\d{2}):(\d{2}$)/;
56  var s_match = regex.exec(start.toString());
57
58  if( s_match == null) {
59   return '';
60  }
61  var yy = s_match[1];
62
63  var mm = parseInt(s_match[2], 10 /** base 10 **/);
64  var dd = s_match[3];
65
66  var s_hh = parseInt(s_match[4], 10 /** base 10 **/);
67
68  if (s_hh > 12) {
69    s_ampm = "PM";
70  } else {
71    s_ampm = "AM";
72  }
73  s_hh = toStr(s_hh);
74  var s_mi = s_match[5];
75
76
77  var str =  getMonthName(mm) + " " +  dd + ", " + yy ;
78  str += " " + s_hh + ":" + s_mi;
79  str += " " + s_ampm;
80
81  regex =  /(^\d{4})-(\d{2})-(\d{2})\s{1}(\d{2}):(\d{2}):(\d{2}$)/;
82  var e_match = regex.exec(end.toString());
83  if( e_match == null) {
84   return str + ' PST';
85  }
86  var e_hh = parseInt(e_match[4], 10 /** base 10 **/);
87  if (e_hh > 12) {
88    e_ampm = "PM";
89  } else {
90    e_ampm = "AM";
91  }
92  e_hh = toStr(e_hh);
93  var e_mi = e_match[5];
94
95  str += " to " + e_hh + ":" + e_mi;
96  str += " " + e_ampm;
97  str += " PST";
98  return str;
99
100}
101
102
103function formatDateUtf(date) {
104  var regex =  /(^\d{4})-(\d{2})-(\d{2})\s{1}(\d{2}):(\d{2}):(\d{2}$)/;
105  var match = regex.exec(date.toString());
106  var yy = match[1];
107  var mm = match[2];
108  var dd = match[3];
109  var hh = parseInt(match[4], 10) + 8;  // +8 to convert from PST to GMT
110  var mi = match[5] + "00"; // add seconds
111
112  if (hh >= 24) {  // If the GMT adjustment put us into the next day,
113    dd++;          // increment the day and
114    hh -= 24;      // set the hour back to real hours
115  }
116  hh = hh < 10 ? "0" + hh : hh;  // form a double digit number for single digit hours
117
118  return yy + mm + dd + 'T' + hh + mi + 'Z';
119}
120