Published
Edited
Jul 14, 2021
Importers
1 star
Insert cell
md`# Time Utils`
Insert cell
import { addDefaults } from '@nuuuwan/option-utils'
Insert cell
SECONDS_IN = Object({
MINUTE: 60,
HOUR: 3600,
DAY: 86400,
WEEK: 86400 * 7,
FORTNIGHT: 86400 * 14,
MONTH: (86400 * 365.25) / 12,
YEAR: 86400 * 365.25
})
Insert cell
SECS_IN = SECONDS_IN
Insert cell
MSECS_IN_SEC = 1000
Insert cell
DAYS_IN_WEEK = 7
Insert cell
MAX_WEEKS_IN_MONTH = 6
Insert cell
MONTH_LIST = [
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December'
]
Insert cell
DAY_LIST = [
'Sunday',
'Monday',
'Tuesday',
'Wednesday',
'Thursday',
'Friday',
'Saturday'
]
Insert cell
md`---`
Insert cell
function getUnixTimeFromDateStr(dateStr) {
return Date.parse(dateStr) / MSECS_IN_SEC;
}
Insert cell
getUnixTimeFromDateStr('2020/01/01')
Insert cell
function getCurrentUnixTime() {
return new Date().getTime() / MSECS_IN_SEC;
}
Insert cell
function formatDate(unixTime, options = {}) {
if (!options['day'] || options['day'] !== 'hide') {
options['day'] = 'numeric';
} else {
delete options['day'];
}
options = addDefaults(options, {
year: 'numeric',
month: 'short'
});
return new Date(unixTime * 1000.0).toLocaleDateString(options);
}
Insert cell
formatDate(getCurrentUnixTime())
Insert cell
function formatTime(unixTime) {
return new Date(unixTime * 1000.0).toLocaleTimeString();
}
Insert cell
formatTime(getCurrentUnixTime())
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more