Skip to content Skip to sidebar Skip to footer

Javascript Date() Is Changing The Date

I'm pulling events from google calendar. I'm able to see an event date: var date = when.split('T')[0]; console.log(date); This outputs 2016-01-07. Then I put it into an array i

Solution 1:

The default timezone when parsing a date is UTC. If you want to use the client timezone you can adjust the date so that it occurs at the right time in the browser's timezone:

var eventDate = new Date(v.eventDate);
var ms_per_minute = 60000;
eventDate.setTime( eventDate.getTime() + eventDate.getTimezoneOffset()*ms_per_minute );

Otherwise you can just use UTC whenever you display the date:

console.log("Show This Date: " + eventDate.toUTCSTring() ); 

Solution 2:

I think moment.js could help you on date manipulation.

Here is a fiddle for u

jQuery(document).ready(function($) {
  var the_date_string = $('#in-date > pre').text();
  var result = moment(the_date_string, 'YYYY-MM-DD'); //here is more info about that http://momentjs.com/docs/#/parsing/string-format/
  $('#out-date > pre').text(result);


});
body {
  font-family: arial;
}
<script src="http://momentjs.com/downloads/moment.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h1 id="in-date"> INPUT: <pre>2016-01-07</pre></h1>
<h1 id="out-date"> OUTPUT: <pre></pre></h1>

Thumbs up if u liked it! :)

[EDIT] If u just want the client timestamp (in internationals formats) you can easly use the moment() function


Solution 3:

I know this is old, but may help somebody.

I solved this issue by adding the time on the Date argument:

var eventDate = new Date(v.eventDate + ' 00:00:00');

You can remove it after


Post a Comment for "Javascript Date() Is Changing The Date"