Basic date object Date object converted into the string doesn't look good. new Date().toString() Sat Aug 24 2024 22:05:05 GMT+0300 (Itä-Euroopan kesäaika) Function
// functions/ddmmyyyyHHMMSS.js
export const ddmmyyyyHHMMSS = (date = new Date(), utc = false) => {
if (isNaN(Date.parse(date))) return 'can not parse date'
const addZeroToNum = (num) => num.toString().length === 1 ? '0' + num : num
const d = new Date(date)
const yyyy = utc ? d.getUTCFullYear() : d.getFullYear()
const mm = addZeroToNum(utc ? d.getUTCMonth() + 1 : d.getMonth() + 1)
const dd = addZeroToNum(utc ? d.getUTCDate() : d.getDate())
const HH = addZeroToNum(utc ? d.getUTCHours() : d.getHours())
const MM = addZeroToNum(utc ? d.getUTCMinutes() : d.getMinutes())
const SS = addZeroToNum(utc ? d.getUTCSeconds() : d.getSeconds())
return `${dd}.${mm}.${yyyy} ${HH}:${MM}:${SS}`
}
Output new Date().toString() Sat Aug 24 2024 22:05:05 GMT+0300 (Itä-Euroopan kesäaika) new Date().toISOString() 2024-08-24T19:05:05.844Z ddmmyyyyHHMMSS(new Date()) 24.08.2024 22:05:05 ddmmyyyyHHMMSS(new Date(), true) 24.08.2024 19:05:05 ddmmyyyyHHMMSS("1999-12-31T15:16:17.340Z") 31.12.1999 17:16:17 ddmmyyyyHHMMSS("1999-12-31T15:16:17.340Z", true) 31.12.1999 15:16:17