Date converted from JSON incorrectly

I am sending 2 date ranges which represent the start and end of this current week. However the conversion of dates are not what I'm expecting on the server side. The start date is fine, but the end date is a day out which I don't quite understand why?

Console.log output

Date {Mon May 13 2013 00:00:00 GMT+0100 (GMT Daylight Time)} 
Date {Sun May 19 2013 23:59:59 GMT+0100 (GMT Daylight Time)}

The JS that creates the above output

var startOfWeek = moment().day(1).hour(0).minute(0).second(0).toDate()
var endOfWeek = moment().day(7).hour(23).minute(59).second(59).toDate()
console.log(startOfWeek, endOfWeek)

JSON

{"Start":"2013-05-12T23:00:00.000Z","End":"2013-05-18T23:00:00.000Z"}

C#

Start: {13/05/2013 00:00:00}
End: {18/05/2013 23:59:59}

Controller

public HttpResponseMessage GetAllCampaignsByDate(DateRange _dateRange)
{
}

Model

public class DateRange
{
    private DateTime m_start;
    private DateTime m_end;

    public DateTime Start
    {
        get{ return m_start; }
        set { m_start = value.ToLocalTime(); }
    }

    public DateTime End
    {
        get { return m_end; }
        set { m_end = value.ToLocalTime().AddSeconds(-1); }
    }
}

Answers


It may have to do with moment.js using ISO8601 (the Z on the end of the string indicates UTC) and then going through ToLocalTime().


This looks like a timezone problem. I am not sure which locale you are on but it appears as though you aren't taking into consideration DST. Make sure you are sending UTC dates from the client & not ones which are local to the client timezone.

Looking at the documentation for moment.js

By default, moment parses and displays in local time

as it's effectively just a wrapper around the Date object in JS, which by default:

creates a JavaScript Date object for today's date and time according to local time

Therefore, you need to tell moment.js to convert the date you have created into UTC using utc parsing e.g.

moment.utc(moment().day(1).hour(0).minute(0).second(0).toDate());

Need Your Help

C# LINQ Query for selecting data from different levels of hierarchy

c# linq hierarchical

I have a hierarchical data arranged in form Groups -> SubGroups -> Items.

Updating the loaded assembly in ASP.Net

asp.net .net-assembly appdomain

After researching, I found the approach for this solution:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.