0

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 inside an object:

allEvents.push({
    eventDate:date,
    eventTime:time,
    eventTBD:TBD
});

Then, when I go to grab the date again:

$.each(allEvents, function(i, v){
    var eventDate = new Date(v.eventDate);
    if(eventDate > startDate && eventDate < endDate){
        console.log(v.eventDate);
        console.log("Show This Date: " + eventDate);    
    }
});

For January, I get this output:

2016-01-07
Show This Date: Wed Jan 06 2016 19:00:00 GMT-0500 (EST)

for March, I get this output:

2016-03-19
Show This Date: Fri Mar 18 2016 20:00:00 GMT-0400 (EDT)

It's showing the day before the date I just showed... It seems to be 5 hours off? Do I need to account for this? How do I do so?

ntgCleaner
  • 5,865
  • 9
  • 48
  • 86

3 Answers3

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

lucianov88
  • 185
  • 3
  • 10
0

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() ); 
Paul
  • 139,544
  • 27
  • 275
  • 264
0

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

Paolo Falomo
  • 427
  • 3
  • 15