Get Time Stamp from datetime - javascript

This code return a datetime stamp string, can be used to file name to make it differnet to avoid override of file.
function timeStamp() {
    var date = new Date();
    var year = date.getFullYear();
    var month = date.getMonth() + 1
    var day = date.getDate();
    var hour = date.getHours();
    var minutes = date.getMinutes();
    var secconds = date.getSeconds()
    var formatted = month + '' + day + '' + year + '' + hour + '' + minutes + '' + secconds;
    console.log(formatted);
}

You can simply copy and paste this function in your code and call it where it is required.
I will explain this function read it till end.

This function call Date function and get the current date object which is a native code of js
Now extract the year,month, day ... Second from date object which has several methods to provide these values for example date.getFullYear() function returns year on current date.

Once all values collected wrap them into string see formatted variable. You can change the position of these variables to format your time in string.

Comments