user3884692
user3884692

Reputation: 105

How to set current date time using JavaScript

I am getting date in the following format :

Thu Jan 01 1970 21:30:00 GMT +0530(IST)

I want to that time will remain and I can change date to the current date like :

Mon Dec 14 2015 21:30:00 GMT +0530(IST)

Can anybody help me ?

Upvotes: 3

Views: 10427

Answers (3)

Sworrub Wehttam
Sworrub Wehttam

Reputation: 598

Reading the comments I think I understand what you're trying to achieve. Try this:

var oldDate = new Date("Thu Jan 01 1970 21:30:00 GMT +0530(IST)");
var newDate = new Date();
newDate.setHours(oldDate.getHours());
newDate.setMinutes(oldDate.getMinutes());
newDate.setSeconds(oldDate.getSeconds());

Then newDate should contain the date you desire.

Upvotes: 5

Snehal Masne
Snehal Masne

Reputation: 3429

Use new Date();

Also have a look at : http://www.w3schools.com/js/js_dates.asp

Upvotes: -1

Venkat.R
Venkat.R

Reputation: 7746

You are getting the Date Object. Use something like below to get custom one.

date.getDay() + '-' + date.getDate() + '-' + (date.getMonth()+1) + '-' + date.getFullYear()

Reference URL:
http://www.w3schools.com/jsref/jsref_getday.asp
http://www.w3schools.com/jsref/jsref_getdate.asp
http://www.w3schools.com/jsref/jsref_getmonth.asp
http://www.w3schools.com/jsref/jsref_getfullyear.asp

Upvotes: -1

Related Questions