UNPKG

1.53 kBJavaScriptView Raw
1/*!
2 * Copyright 2016 Amazon.com,
3 * Inc. or its affiliates. All Rights Reserved.
4 *
5 * Licensed under the Amazon Software License (the "License").
6 * You may not use this file except in compliance with the
7 * License. A copy of the License is located at
8 *
9 * http://aws.amazon.com/asl/
10 *
11 * or in the "license" file accompanying this file. This file is
12 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
13 * CONDITIONS OF ANY KIND, express or implied. See the License
14 * for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18const monthNames = [
19 'Jan',
20 'Feb',
21 'Mar',
22 'Apr',
23 'May',
24 'Jun',
25 'Jul',
26 'Aug',
27 'Sep',
28 'Oct',
29 'Nov',
30 'Dec',
31];
32const weekNames = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
33
34/** @class */
35export default class DateHelper {
36 /**
37 * @returns {string} The current time in "ddd MMM D HH:mm:ss UTC YYYY" format.
38 */
39 getNowString() {
40 const now = new Date();
41
42 const weekDay = weekNames[now.getUTCDay()];
43 const month = monthNames[now.getUTCMonth()];
44 const day = now.getUTCDate();
45
46 let hours = now.getUTCHours();
47 if (hours < 10) {
48 hours = `0${hours}`;
49 }
50
51 let minutes = now.getUTCMinutes();
52 if (minutes < 10) {
53 minutes = `0${minutes}`;
54 }
55
56 let seconds = now.getUTCSeconds();
57 if (seconds < 10) {
58 seconds = `0${seconds}`;
59 }
60
61 const year = now.getUTCFullYear();
62
63 // ddd MMM D HH:mm:ss UTC YYYY
64 const dateNow = `${weekDay} ${month} ${day} ${hours}:${minutes}:${seconds} UTC ${year}`;
65
66 return dateNow;
67 }
68}