Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Get User’s local timestamp that can be utilized client or for inclusion on strings to be sent to backend.
/**
* getUsersLocalTimestamp --- Get User’s local timestamp for inclusion on strings sent to backend.
* @returns {string} "2020-06-03 @ 12:12:25"
* @usage getUsersLocalTimestamp()
*/
function getUsersLocalTimestamp() {
let date = new Date();
let yyyy = date.getFullYear().toString();
let mm = (date.getMonth() + 1).toString();
let dd = date.getDate().toString();
return yyyy + '-' + (mm[1] ? mm : "0" + mm[0]) + '-' + (dd[1] ? dd : "0" + dd[0]) + ' @ ' + date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment