MCPcopy
hub / github.com/tensorflow/tfjs-examples / parseDateTime_

Method parseDateTime_

jena-weather/data.js:127–147  ·  view source on GitHub ↗

* Parse the date-time string from the Jena weather CSV file. * * @param {*} str The date time string with a format that looks like: * "17.01.2009 22:10:00" * @returns date: A JavaScript Date object. * normalizedDayOfYear: Day of the year, normalized between 0 and 1. *

(str)

Source from the content-addressed store, hash-verified

125 * normalizedTimeOfDay: Time of the day, normalized between 0 and 1.
126 */
127 parseDateTime_(str) {
128 const items = str.split(' ');
129 const dateStr = items[0];
130 const dateStrItems = dateStr.split('.');
131 const day = +dateStrItems[0];
132 const month = +dateStrItems[1] - 1; // month is 0-based in JS `Date` class.
133 const year = +dateStrItems[2];
134
135 const timeStrItems = items[1].split(':');
136 const hours = +timeStrItems[0];
137 const minutes = +timeStrItems[1];
138 const seconds = +timeStrItems[2];
139
140 const date = new Date(Date.UTC(year, month, day, hours, minutes, seconds));
141 const yearOnset = new Date(year, 0, 1);
142 const normalizedDayOfYear =
143 (date - yearOnset) / (366 * 1000 * 60 * 60 * 24);
144 const dayOnset = new Date(year, month, day);
145 const normalizedTimeOfDay = (date - dayOnset) / (1000 * 60 * 60 * 24)
146 return {date, normalizedDayOfYear, normalizedTimeOfDay};
147 }
148
149
150 /**

Callers 1

loadMethod · 0.95

Calls

no outgoing calls

Tested by

no test coverage detected