Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting current date and time in JavaScript

I have a script that prints the current date and time in JavaScript, but the DATE is always wrong. Here is the code:

var currentdate = new Date(); var datetime = "Last Sync: " + currentdate.getDay() + "/" + currentdate.getMonth()  + "/" + currentdate.getFullYear() + " @ "  + currentdate.getHours() + ":"  + currentdate.getMinutes() + ":" + currentdate.getSeconds(); 

It should print 18/04/2012 15:07:33 and prints 3/3/2012 15:07:33

like image 333
Ricardo Avatar asked Apr 18 '12 14:04

Ricardo


People also ask

How can I get the current Date and time?

The Clock. systemUTC(). instant() method returns the current date and time both.

How do I get the current Date and time in HTML?

Use Date. now() to get the current timestamp fast.


2 Answers

.getMonth() returns a zero-based number so to get the correct month you need to add 1, so calling .getMonth() in may will return 4 and not 5.

So in your code we can use currentdate.getMonth()+1 to output the correct value. In addition:

  • .getDate() returns the day of the month <- this is the one you want
  • .getDay() is a separate method of the Date object which will return an integer representing the current day of the week (0-6) 0 == Sunday etc

so your code should look like this:

var currentdate = new Date();  var datetime = "Last Sync: " + currentdate.getDate() + "/"                 + (currentdate.getMonth()+1)  + "/"                  + currentdate.getFullYear() + " @ "                   + currentdate.getHours() + ":"                   + currentdate.getMinutes() + ":"                  + currentdate.getSeconds(); 

JavaScript Date instances inherit from Date.prototype. You can modify the constructor's prototype object to affect properties and methods inherited by JavaScript Date instances

You can make use of the Date prototype object to create a new method which will return today's date and time. These new methods or properties will be inherited by all instances of the Date object thus making it especially useful if you need to re-use this functionality.

// For todays date; Date.prototype.today = function () {      return ((this.getDate() < 10)?"0":"") + this.getDate() +"/"+(((this.getMonth()+1) < 10)?"0":"") + (this.getMonth()+1) +"/"+ this.getFullYear(); }  // For the time now Date.prototype.timeNow = function () {      return ((this.getHours() < 10)?"0":"") + this.getHours() +":"+ ((this.getMinutes() < 10)?"0":"") + this.getMinutes() +":"+ ((this.getSeconds() < 10)?"0":"") + this.getSeconds(); } 

You can then simply retrieve the date and time by doing the following:

var newDate = new Date(); var datetime = "LastSync: " + newDate.today() + " @ " + newDate.timeNow(); 

Or call the method inline so it would simply be -

var datetime = "LastSync: " + new Date().today() + " @ " + new Date().timeNow(); 
like image 104
Mark Walters Avatar answered Oct 22 '22 00:10

Mark Walters


To get time and date you should use

    new Date().toLocaleString();  >> "09/08/2014, 2:35:56 AM" 

To get only the date you should use

    new Date().toLocaleDateString();  >> "09/08/2014" 

To get only the time you should use

    new Date().toLocaleTimeString();  >> "2:35:56 AM" 

Or if you just want the time in the format hh:mm without AM/PM for US English

    new Date().toLocaleTimeString('en-US', { hour12: false,                                               hour: "numeric",                                               minute: "numeric"}); >> "02:35" 

or for British English

    new Date().toLocaleTimeString('en-GB', { hour: "numeric",                                               minute: "numeric"});  >> "02:35" 

Read more here.

like image 44
Chhorn Elit Avatar answered Oct 22 '22 00:10

Chhorn Elit