logesh
logesh

Reputation: 2672

How to change the date string to date time with local timezone in js?

I use date picker and i get the value of the date picker as

document.getElementById("id_of_datepicker")

and when i change this to date using

new Date(document.getElementById("id_of_datepicker"));

it returns

Thu Feb 06 2014 05:30:00 GMT+0530 (IST)

and what i look for is instead of passing the dates as i do i need to send it in local timezone with 00 hours, min, secs which then has to be converted to utc. how can i do this? please help me.

Upvotes: 1

Views: 465

Answers (3)

Mehran Hatami
Mehran Hatami

Reputation: 12961

you have to split it manually and use Date.UTC for utc function like this:

var dateStr = document.getElementById("id_of_datepicker");
var dateArr = dateStr.split(/[\-T:]/);//suppose it is like 2014-06-12T00:00:00
var localTime=new Date(Date.parse(dateArr[0],dateArr[1],dateArr[2]), 0, 0, 0)
var utcTime=new Date(Date.UTC(dateArr[0],dateArr[1],dateArr[2]), 0, 0, 0)

Upvotes: 1

ajitksharma
ajitksharma

Reputation: 2019

Suppose this the dt value returned by the date object Thu Feb 06 2014 05:30:00 GMT+0530 (IST)

 Date dt=new Date();
 dat=dt.getFullYear()+'-'+dt.getMonth()+'-'+dt.getDate();

It will return date dt print in yyyy-mm-dd format ot you can change in your desired format

Upvotes: 0

Alex Art.
Alex Art.

Reputation: 8781

You could try using moment.js for date/time manipulation

Upvotes: 0

Related Questions