Is there a way to get date and time in JS? I searched in google and saw ways to get the date appart and time appart but not together. I have a field that calls "Date Time" and i need to show data -12 hours so i need to figure out how to save datetime. Can someone help?
Asked
Active
Viewed 74 times
-3
-
possible duplicate of [How to get current date in JavaScript?](http://stackoverflow.com/questions/1531093/how-to-get-current-date-in-javascript) – Robert Rossmann Feb 18 '15 at 08:31
-
You should check the Date object [documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date?redirectlocale=en-US&redirectslug=JavaScript%2FReference%2FGlobal_Objects%2FDate) first. You have plenty examples there. – Razvan Feb 18 '15 at 08:32
-
A good example on Mozilla's Developer Network: [Global objects / Date](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date) – Robert Rossmann Feb 18 '15 at 08:33
3 Answers
0
Use javaScript Date object to work with date and time.
alert(new Date());

Md Ashaduzzaman
- 4,032
- 2
- 18
- 34
0
Use new Date();
var x = new Date();
You can also fetch each value using .getDay()
, .getMonth()
, .getYear()
, .getHours()
, .getMinutes()
, .getSeconds()
Like
x.getHours(); // Will give you hours.

void
- 36,090
- 8
- 62
- 107
0
use date object of javascript then adjust your date and time combinations for example, create a function getdateTime which will return dateTime combinations
function getdateTime() {
var date = new Date();
var dataVal=date.getDate() +"/"+ (date.getMonth() + 1)+"/" + date.getFullYear() + ":" + date.getHours() + ":" + date.getMinutes();
return dataVal;
}
var datetime=getdateTime();
console.log("datetime:" +datetime);
output for example : datetime:18/2/2015:14:26
ie current date and time
since months starts from 0 to 11 you have to add 1 to get current month.
you can use other date functions depending upon your requirements.

Indrani Sen
- 287
- 4
- 19