How do I get the current time only in JavaScript
var d = new Date("2011-04-20T09:30:51.01");
d.getHours(); // => 9
d.getMinutes(); // => 30
d.getSeconds(); // => 51
or
var d = new Date(); // for now
d.getHours(); // => 9
d.getMinutes(); // => 30
d.getSeconds(); // => 51
Getting current date and time in JavaScript
.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 theDate
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();
How to show current time in JavaScript in the format HH:MM:SS?
function checkTime(i) { if (i < 10) { i = "0" + i; } return i;}
function startTime() { var today = new Date(); var h = today.getHours(); var m = today.getMinutes(); var s = today.getSeconds(); // add a zero in front of numbers<10 m = checkTime(m); s = checkTime(s); document.getElementById('time').innerHTML = h + ":" + m + ":" + s; t = setTimeout(function() { startTime() }, 500);}startTime();
<div id="time"></div>
How do I get the current date in JavaScript?
Use new Date()
to generate a new Date
object containing the current date and time.
var today = new Date();var dd = String(today.getDate()).padStart(2, '0');var mm = String(today.getMonth() + 1).padStart(2, '0'); //January is 0!var yyyy = today.getFullYear();
today = mm + '/' + dd + '/' + yyyy;document.write(today);
How to get the current date or/and time in seconds
var seconds = new Date().getTime() / 1000;
....will give you the seconds since midnight, 1 Jan 1970
Reference
Current time formatting with Javascript
A JavaScript Date has several methods allowing you to extract its parts:
getFullYear()
- Returns the 4-digit yeargetMonth()
- Returns a zero-based integer (0-11) representing the month of the year.getDate()
- Returns the day of the month (1-31).getDay()
- Returns the day of the week (0-6). 0 is Sunday, 6 is Saturday.getHours()
- Returns the hour of the day (0-23).getMinutes()
- Returns the minute (0-59).getSeconds()
- Returns the second (0-59).getMilliseconds()
- Returns the milliseconds (0-999).getTimezoneOffset()
- Returns the number of minutes between the machine local time and UTC.
There are no built-in methods allowing you to get localized strings like "Friday", "February", or "PM". You have to code that yourself. To get the string you want, you at least need to store string representations of days and months:
var months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
var days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
Then, put it together using the methods above:
var months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];var days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];var d = new Date();var day = days[d.getDay()];var hr = d.getHours();var min = d.getMinutes();if (min < 10) { min = "0" + min;}var ampm = "am";if( hr > 12 ) { hr -= 12; ampm = "pm";}var date = d.getDate();var month = months[d.getMonth()];var year = d.getFullYear();var x = document.getElementById("time");x.innerHTML = day + " " + hr + ":" + min + ampm + " " + date + " " + month + " " + year;
<span id="time"></span>
How to get current time with jQuery
You may try like this:
new Date($.now());
Also using Javascript you can do like this:
var dt = new Date();var time = dt.getHours() + ":" + dt.getMinutes() + ":" + dt.getSeconds();document.write(time);
Javascript to display the current date and time
Try this:
var d = new Date(),
minutes = d.getMinutes().toString().length == 1 ? '0'+d.getMinutes() : d.getMinutes(),
hours = d.getHours().toString().length == 1 ? '0'+d.getHours() : d.getHours(),
ampm = d.getHours() >= 12 ? 'pm' : 'am',
months = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'],
days = ['Sun','Mon','Tue','Wed','Thu','Fri','Sat'];
return days[d.getDay()]+' '+months[d.getMonth()]+' '+d.getDate()+' '+d.getFullYear()+' '+hours+':'+minutes+ampm;
DEMO
Related Topics
How to Get Subarray from Array
Replacing Spaces with Underscores in JavaScript
Convert Js Date Time to MySQL Datetime
What Is the Meaning of Symbol $ in Jquery
Calculate Date from Week Number in JavaScript
Fetch: Reject Promise with JSON Error Object
Sort Objects in an Array Alphabetically on One Property of the Array
Associative Array Versus Object in JavaScript
JavaScript Infinitely Looping Slideshow with Delays
Break Promise Chain and Call a Function Based on the Step in the Chain Where It Is Broken (Rejected)
Jquery .On() Method with Multiple Event Handlers to One Selector
How to Find a Number in a String Using JavaScript
Uncaught Typeerror: Cannot Read Property 'Value' of Undefined
Scaling Socket.Io to Multiple Node.Js Processes Using Cluster
Using a Variable in Mongodb Update
Uncaught Typeerror: Cannot Read Property 'Value' of Null
Invariant Violation: _Registercomponent(...): Target Container Is Not a Dom Element