Skip to content

Instantly share code, notes, and snippets.

@mholtzhausen
Last active October 5, 2021 11:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mholtzhausen/aab982a0eb36d285608ed5af107ffca8 to your computer and use it in GitHub Desktop.
Save mholtzhausen/aab982a0eb36d285608ed5af107ffca8 to your computer and use it in GitHub Desktop.
DateTime Formatting made very very simple

DateUtil

A very simple local-time only date formatting tool that lives in one file and does the minimum for mvp.

How to use

const {format} = require('./dateUtil.js')

let date=Date.now()
let formatStr='www, DD mmmm, YYYY H:I:s.SS'

// Directly format 
console.log( format( formatStr , date ) )

// Register a format
format.quick.register('shorty','YYYY-M-D H:I:S')

// Use the registered Format
console.log( format.quick('shorty',date))
const months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
const days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
const parse = (d) => {
if (!d) return new Date(Date.now())
if (d instanceof Date) return d
if (!isNaN(d) && `${d}`.length == 13) return new Date(d)
return new Date(Date.parse(d))
}
const format = (formatStr, date) => {
const pad = n => `${n < 10 ? '0' : ''}${n}`
date = parse(date)
let day = date.getDate()
let dow = date.getDay()
let year = date.getFullYear()
let month = date.getMonth()
let hours = date.getHours()
let minutes = date.getMinutes()
let seconds = date.getSeconds()
let ms = date.getMilliseconds()
let pm = hours > 12 ? true : false
let out = `${formatStr}`
out = out.replace(/YYYY/g, year) // 1921
out = out.replace(/YY/g, `${year}`.slice(-2)) // 21 Year
out = out.replace(/DD/g, pad(day)) // 08 Day
out = out.replace(/D/g, day) // 8 Day
out = out.replace(/AM/g, pm ? 'PM' : 'AM') // AM/PM
out = out.replace(/Am/g, pm ? 'Pm' : 'Am') // Am/Pm
out = out.replace(/am/g, pm ? 'pm' : 'am') // am/pm
out = out.replace(/HH/g, pad(hours)) // 04 14 hours
out = out.replace(/H/g, hours) // 4 14 hours
out = out.replace(/hh/g, pad(pm ? hours - 12 : hours)) // 04 02 hours
out = out.replace(/h/g, pm ? hours - 12 : hours) // 4 2 hours
out = out.replace(/II/g, pad(minutes)) // 08 minutes
out = out.replace(/I/g, minutes) // 8 minutes
out = out.replace(/ss/g, pad(seconds)) // 08 seconds
out = out.replace(/s/g, seconds) // 8 seconds
out = out.replace(/SS/g, `${ms < 100 ? '0' : ''}${ms < 10 ? '0' : ''}${ms}`) // 002 ms
out = out.replace(/S/g, ms) // 2 ms
out = out.replace(/WWW/g, days[dow].toUpperCase()) // MONDAY
out = out.replace(/www/g, days[dow]) // Monday
out = out.replace(/WW/g, days[dow].slice(0, 3).toUpperCase()) //MON
out = out.replace(/ww/g, days[dow].slice(0, 3)) // Mon
out = out.replace(/W/g, days[dow].slice(0, 2).toUpperCase()) //MO
out = out.replace(/w/g, days[dow].slice(0, 2)) // Mo
out = out.replace(/MMMM/g, months[month].toUpperCase()) // MARCH
out = out.replace(/mmmm/g, months[month]) // March
out = out.replace(/MMM/g, months[month].slice(0, 3).toUpperCase()) // MAR
out = out.replace(/mmm/g, months[month].slice(0, 3)) // Mar
out = out.replace(/MM/g, pad(month+1)) // 03 Month
out = out.replace(/M/g, month+1) // 3 Month
return out
}
format.quick = (formatName, date) => {
return format(format.quick.registry[formatName], date)
}
format.quick.registry = {}
format.quick.register = (formatName, formatStr) => {
format.quick.registry[formatName] = formatStr
}
format.quick.register('datetime-local', `YYYY-MM-DDTH:I`)
format.quick.register('iso', `YYYY-MM-DDTH:II:ss.SZ`)
module.exports = { parse, format, months, days }
console.log(format.quick('datetime-local'))
console.log(format.quick('full'))
console.log(format.quick('iso'))
console.log((new Date()).toISOString())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment