Skip to content

Instantly share code, notes, and snippets.

@Shibily-kms
Last active September 11, 2023 09:30
Show Gist options
  • Save Shibily-kms/ecbbd6286ace4bc4c0403bb09ef0d561 to your computer and use it in GitHub Desktop.
Save Shibily-kms/ecbbd6286ace4bc4c0403bb09ef0d561 to your computer and use it in GitHub Desktop.
JS Date Time Formats
// ISO time to YYYYMMDD formate using JavaScript
const YYYYMMDDFormat = (ISOdate, symbol = '-') => {
symbol = symbol ? symbol : ''
const year = ISOdate.getFullYear();
const month = String(ISOdate.getMonth() + 1).padStart(2, '0');
const day = String(ISOdate.getDate()).padStart(2, '0');
return `${year}${symbol}${month}${symbol}${day}`;
}
---------------------------------------------------------------
// 24 Hours to Local time (14:15:23 ===> 02:15:23 PM)
function stringToLocalTime(time = '', ifSecond = false) { // 14:15:23 ==> 02:15 PM
if (!time) {
return
}
const [hours, minutes, seconds] = time.split(':')
const suffix = parseInt(hours, 10) >= 12 ? 'PM' : 'AM';
let hours12Format = (parseInt(hours, 10) % 12) || 12;
hours12Format = hours12Format < 10 ? `0${hours12Format}` : hours12Format
if (seconds && ifSecond) {
return `${hours12Format}:${minutes}:${seconds} ${suffix}`;
}
return `${hours12Format}:${minutes} ${suffix}`;
}
-----------------------------------------------------------------
// Covert second to Hours (10h 30m)
const getTimeFromSecond = (seconds) => {
const hours = Math.floor(seconds / 3600);
const minutes = Math.floor((seconds % 3600) / 60);
let time = ''
if (hours) {
time = `${hours}h `
}
if (minutes) {
time = time + `${minutes}m`
}
return time;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment