UNPKG

1.61 kBJavaScriptView Raw
1import { dateToDash } from '@valjoux/convert';
2import { formatDateTime } from '@valjoux/format-date-time';
3import { formatTime } from '@valjoux/format-time';
4
5const padMilli = ms => (ms = '' + ms).length > 2 ? ms : ('00' + ms).slice(-3);
6const milli = date => padMilli(date.getMilliseconds());
7
8/** @return {string} - YYYY-MM-DD */
9
10const date = (dt = new Date(), dash = '-') => dateToDash(dt, dash);
11/** @returns {string} - hh:mm:ss */
12
13const roughTime = (date = new Date()) => formatTime(date);
14/** @returns {string} - hh:mm:ss.mmm */
15
16const time = (date = new Date()) => formatTime(date) + '.' + milli(date);
17/** @return {string} - mm/dd/yy, hh:mm:ss */
18
19const dateTime = (date = new Date()) => formatDateTime(date);
20/** @returns {number} - YYYY */
21
22const year = (date = new Date()) => date.getFullYear();
23/**
24 * @return {string} - YYYY-MM-DD
25 * @deprecated use date() instead
26 */
27
28const today = (dash = '-') => dateToDash(new Date(), dash);
29/**
30 * @returns {string} - hh:mm:ss
31 * @deprecated use roughTime() instead
32 * */
33
34const roughly = (date = new Date()) => formatTime(date);
35/**
36 * @return {string} - hh:mm:ss
37 * @deprecated use roughTime() instead
38 * */
39
40const roughlyNow = () => formatTime(new Date());
41/**
42 * @return {string} - hh:mm:ss.mmm
43 * @deprecated use time() instead
44 * */
45
46const now = () => {
47 var _Date;
48
49 return _Date = new Date(), time(_Date);
50};
51/**
52 * @return {string} - mm/dd/yy, hh:mm:ss
53 * @deprecated use dateTime() instead
54 * */
55
56const present = () => formatDateTime(new Date());
57
58export { date, dateTime, now, present, roughTime, roughly, roughlyNow, time, today, year };