Skip to content

Instantly share code, notes, and snippets.

@0187773933
Last active June 16, 2022 23:43
Show Gist options
  • Save 0187773933/af8cc0931db053bc07e67a3a30389458 to your computer and use it in GitHub Desktop.
Save 0187773933/af8cc0931db053bc07e67a3a30389458 to your computer and use it in GitHub Desktop.
Get Common Time String in Javascript
const MonthNames = [ "JAN" , "FEB" , "MAR" , "APR" , "MAY" , "JUN" , "JUL" , "AUG" , "SEP" , "OCT" , "NOV" , "DEC" ];
function get_time_string() {
let today = new Date();
const milliseconds = String( today.getMilliseconds() ).padStart( 3 , "0" );
today = new Date( today.toLocaleString( "en-US" , { timeZone: "America/New_York" } ) );
const day = String( today.getDate() ).padStart( 2 , "0" ) ;
const month = MonthNames[ today.getMonth() ];
const year = today.getFullYear();
const hours = String( today.getHours() ).padStart( 2 , "0" );
const minutes = String( today.getMinutes() ).padStart( 2 , "0" );
const seconds = String( today.getSeconds() ).padStart( 2 , "0" );
const result = `${hours}:${minutes}:${seconds}.${milliseconds}`;
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment