Skip to content

Instantly share code, notes, and snippets.

@wanderrful
Forked from jonkemp/timestamp.js
Last active March 11, 2020 07:03
Show Gist options
  • Save wanderrful/660b83d459892e304ed2b87285c2d8b6 to your computer and use it in GitHub Desktop.
Save wanderrful/660b83d459892e304ed2b87285c2d8b6 to your computer and use it in GitHub Desktop.
Print out a nicely formatted timestamp in TypeScript.
export function fn_getTimeStamp(): Object {
// Create a date object with the current time
let now: Date = new Date();
// Create an array with the current month, day and time
let date: Array<String> = [ String(now.getMonth() + 1), String(now.getDate()), String(now.getFullYear()) ];
// Create an array with the current hour, minute and second
let time: Array<String> = [ String(now.getHours()), String(now.getMinutes())];
// If seconds and minutes are less than 10, add a zero
for (let i of time) {
if ( Number(i) < 10 ) {
i = "0" + i;
}
}
// Return the formatted string
return {
date: date.join("/"),
time: time.join(":")
};
}
@ricvit
Copy link

ricvit commented Jul 25, 2019

IMO it must be:

export function fn_getTimeStamp(): Object {
    // Create a date object with the current time
    let now: Date = new Date();
    // Create an array with the current month, day and time
    let date: Array<String> = [ String(now.getMonth() + 1), String(now.getDay()), String(now.getFullYear()) ];
    // Create an array with the current hour, minute and second
    let time: Array<String> = [ String(now.getHours()), String(now.getMinutes()), String(now.getSeconds())];
    // Return the formatted string
    return { 
        date: date.join("/"),
        time: time.join(":")
    };
}

@DrBash
Copy link

DrBash commented Mar 11, 2020

Get Day will return you the workday of the current week (e.g. Monday will always be 1st no matter of its the first or 15th of the month). To get the day of the month you need to use now.getDate()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment