Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create date in YYYYMMDDHHMMSS format using javascript? [closed]

Tags:

javascript

How to create the date in YYYYMMDDHHMMSS format using JavaScript ? For example, I want to get the date as 20131018064838.

like image 600
Raheeb M Avatar asked Oct 18 '13 11:10

Raheeb M


People also ask

What date format is DD MMM YYYY JavaScript?

There is no native format in JavaScript for” dd-mmm-yyyy”. To get the date format “dd-mmm-yyyy”, we are going to use regular expression in JavaScript. The regular expression in JavaScript is used to find the pattern in a string. So we are going to find the “dd-mmm-yyyy” pattern in the string using match() method.

What is the format of date in JavaScript?

The preferred Javascript date formats are: Dates Only — YYYY-MM-DD. Dates With Times — YYYY-MM-DDTHH:MM:SSZ.

What format is date now ()?

now() The static Date. now() method returns the number of milliseconds elapsed since January 1, 1970 00:00:00 UTC.


3 Answers

var date = new Date();

alert( date.getFullYear() + ("0" + (date.getMonth() + 1)).slice(-2) + ("0" + date.getDate()).slice(-2) + ("0" + date.getHours() ).slice(-2) + ("0" + date.getMinutes()).slice(-2) + ("0" + date.getSeconds()).slice(-2) );

edit

function pad2(n) { return n < 10 ? '0' + n : n }

var date = new Date();
    
alert( date.getFullYear().toString() + pad2(date.getMonth() + 1) + pad2( date.getDate()) + pad2( date.getHours() ) + pad2( date.getMinutes() ) + pad2( date.getSeconds() ) );
like image 70
gurvinder372 Avatar answered Oct 27 '22 21:10

gurvinder372


Here's my (ES5 safe) method to add the YYYYMMDDHHMMSS() function to any Date object.

On older browsers, either shim Object.defineProperty or just add the inner function directly to Date.prototype:

Object.defineProperty(Date.prototype, 'YYYYMMDDHHMMSS', {
    value: function() {
        function pad2(n) {  // always returns a string
            return (n < 10 ? '0' : '') + n;
        }

        return this.getFullYear() +
               pad2(this.getMonth() + 1) + 
               pad2(this.getDate()) +
               pad2(this.getHours()) +
               pad2(this.getMinutes()) +
               pad2(this.getSeconds());
    }
});
like image 10
Alnitak Avatar answered Oct 27 '22 23:10

Alnitak


Please try using prototype method as following.

<script type="text/javascript">

    Date.prototype.YYYYMMDDHHMMSS = function () {
        var yyyy = this.getFullYear().toString();
        var MM = pad(this.getMonth() + 1,2);
        var dd = pad(this.getDate(), 2);
        var hh = pad(this.getHours(), 2);
        var mm = pad(this.getMinutes(), 2)
        var ss = pad(this.getSeconds(), 2)

        return yyyy + MM + dd+  hh + mm + ss;
    };

    function getDate() {
        d = new Date();
        alert(d.YYYYMMDDHHMMSS());
    }

    function pad(number, length) {
        var str = '' + number;
        while (str.length < length) {
            str = '0' + str;
        }
        return str;
    }
</script>
like image 6
mit Avatar answered Oct 27 '22 22:10

mit