Fullcalendar displayed in wrong time

Asked

Viewed 599 times

3

I’m implementing the FullCalendar in a project, I inserted some Events in the database and return them via Json. But when displaying them on the screen, they come with the correct time date, but the display does not show in the right place.

Example, the data comes with the start date of the event at 06:00, but is displayed at 10:00 am.

In the image below, the event is scheduled to start at 06:30, and end at 07:00.

inserir a descrição da imagem aqui

But it shows on the screen that the time is 10:30 inserir a descrição da imagem aqui

My model Eventos:

 public class Eventos
 {
    [Key]
    public int ID { get; set; }
    public string title { get; set; }
    public DateTime start { get; set; }
    public DateTime? end { get; set; }
    public int StatusEnum { get; set; }
 }

My method that searches the data is like this:

public JsonResult ObterEventos(string start, string end)
{
    var db = new AgendaOnlineFc();
    var dtInicial = Convert.ToDateTime(start).Date;
    var dtfinal = Convert.ToDateTime(end).Date;
    var lista = db.Eventos
        .Where(d => d.end < dtfinal && d.start > dtInicial)
        .ToList();
    return Json(lista, JsonRequestBehavior.AllowGet);
}

The return of mine Json is like this:

{ID: 10, title: "teste", start: "/Date(1495449000000)/", end: "/Date(1495450800000)/", StatusEnum: 0}
ID:10
StatusEnum:0
end:"/Date(1495450800000)/"
start:"/Date(1495449000000)/"
title:"teste"

and my Script,

$('#calendar').fullCalendar({
    events: '/Home/ObterEventos/'
});

Is there any additional configuration to be made ? Or if not where I’m missing in this implementation ?

  • Hello Renan, please add a print showing how the date is coming to the browser

1 answer

4

This is wrong Timezone problem. I imported the dates here (my time is GMT-5) and had different values too.

A package that fixes that is the Moment Timezone. I did the following test and printed the dates at the right time (what you registered in bank):

> moment("/Date(1495449000000)/").tz('America/Sao_Paulo').format("YYYY-MM-DD HH:mm:ss")
< "2017-05-22 06:30:00"

EDIT

You can also set on Controller ancestral (method Initialize) the following configuration, if you want C# to do the conversion for you:

JsonConvert.DefaultSettings = () => new JsonSerializerSettings
{
    DateFormatHandling = DateFormatHandling.IsoDateFormat,
    DateTimeZoneHandling = DateTimeZoneHandling.Local
};

Browser other questions tagged

You are not signed in. Login or sign up in order to post.