PDA

View Full Version : [CLOSED] Problem with dates



supera
Jul 18, 2013, 1:01 PM

supera
Jul 18, 2013, 1:05 PM

Daniil
Jul 18, 2013, 4:53 PM
Hi @supera,

If you would deal wit the latest Ext.NET sources from the SVN trunk, then, I guess, this should work.
http://forums.ext.net/showthread.php?24696#post108759

I understand that you are with v2.1, but could you test your application with that thing and the Ext.NET trunk? Just to know for sure it helps or not.

If it doesn't help, please provide a test case to reproduce.

If it helps, we will think how to get it working with v2.1.

supera
Jul 22, 2013, 12:27 PM
Hi Daniil!

I updated the ext.net from svn/trunk...

I insert this command in global.asax, in application start



Ext.Net.JSON.GlobalSettings.DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.Local


But the issue still occurs...

In time.. when I'm run the application in Visual Studio, Works fine!
When I published in host server, I have the problem... host server is Arvixe.com...
I think that JSON gets the timezone of the host server...
How I can force the timezone to local machine? Is the code above?

Thanks for any help!

Daniil
Jul 22, 2013, 3:55 PM
I think that JSON gets the timezone of the host server...
How I can force the timezone to local machine? Is the code above?


Well, it is what this setting is supposed to do.

Ext.Net.JSON.GlobalSettings.DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.Local

How do you serialize the dates? How do you bind the data to the Store? Could you, please, provide a sample?

supera
Jul 23, 2013, 5:23 PM

Daniil
Jul 23, 2013, 5:54 PM
We just need to understand better how to you bind the data.

Please post a WebMethod which you refers in the AjaxProxy's Url.

supera
Jul 23, 2013, 8:36 PM

Baidaly
Jul 24, 2013, 12:27 AM
Hello!

Thanks for your information. Take a look at this picture that I have in Toronto using your sample: 6603

I think your problem in definition of MS format: UTC milliseconds since epoch

So, for different time zones it will be different and I think on your server and your local computer you have different time zones.

What I would recommend is to use another format for deserialization.

Daniil
Jul 24, 2013, 6:45 AM
Just to clarify something.

We (Ext.NET) don't affect how a WebService deserialize a response. So, this setting

Ext.Net.JSON.GlobalSettings.DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.Local
doesn't make sense in your case. I was just unsure how you bind the data.

This time zone issue is quite a common problem. These links can shed some light on the topic.
http://blog.scosby.com/post/2013/03/01/Serializing-A-DataSet-With-DateTime-Columns.aspx
http://social.msdn.microsoft.com/Forums/vstudio/en-US/a7734650-5080-45bd-a53c-4a41b87dd3a6/is-there-anyway-to-tell-a-web-service-to-return-not-adjust-returned-date-based-on-timezone

supera
Jul 25, 2013, 7:32 PM

Baidaly
Jul 26, 2013, 4:01 AM
As you can read in this thread: http://social.msdn.microsoft.com/Forums/vstudio/en-US/a7734650-5080-45bd-a53c-4a41b87dd3a6/is-there-anyway-to-tell-a-web-service-to-return-not-adjust-returned-date-based-on-timezone


The problem is that I don't care about time, just want the date (short date i.e. 5/31/2011). In my case the date is stored as 5/31/2011 0:00:000.00 in SQL. When my web service is called from a timezone that is -3 hours from the servers timezone, the client's date becomes 5/30/2011 9:00 PM and the client code is using the short date part only so 5/31/2011 is now 5/30/2011. This is obviously causing problems.

This guy has the same problem as you. So his solution was:


turn the date into a string on the service side. This string will use the server's timezone. And since now it is merely a string, the client will see it as is. You can do that inside your service. You don't need to modify the database.

Try to apply it. If it will not work please try to test using our databases from Examples.

supera
Jul 26, 2013, 12:53 PM

Daniil
Jul 26, 2013, 1:09 PM
Is the grid empty at all or the DATAHORA column only?

Please try to replace

{ name: 'DATAHORA', type: 'date', dateFormat: "MS" }
with

{ name: 'DATAHORA', type: 'date', dateFormat: "c" }

supera
Jul 26, 2013, 1:13 PM
No! All grid is empty... No records loaded in grid!

Thanks

Daniil
Jul 26, 2013, 2:11 PM
You tell me that:

We (Ext.NET) don't affect how a WebService deserialize a response

I still do not understand the issue... the problem is that the ASP.NET serializes the paging object or is another lib of Ext.net that does this???


ASP.NET WebService deserializes it and we cannot affect it. You are not confined to use the Paging class, but you will get the same result without it.

This is also not a solution

Return Ext.Net.JSON.Serialize(paging)
because an AjaxProxy's Reader gets just a string this way, not an array of records.

I think the following could be a solution.

[WebMethod]
public object GetData()
{
return new
{
data = JSON.Serialize(data), // not a Paging instance here. In your case it should be "list.AsEnumerable" which you passed to the Paging constructor in the DataTableToPaging method
total = 2
};
}

Here is a full example.

Page

<%@ Page Language="C#" %>

<%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>

<!DOCTYPE html>
<html>
<head runat="server">
<title>Ext.NET v2 Example</title>
</head>
<body>
<form runat="server">
<ext:ResourceManager runat="server" />
<ext:GridPanel ID="GridPanel1" runat="server">
<Store>
<ext:Store runat="server">
<Model>
<ext:Model runat="server">
<Fields>
<ext:ModelField Name="test" />
<ext:ModelField Name="date" Type="Date" />
</Fields>
</ext:Model>
</Model>
<Proxy>
<ext:AjaxProxy Url="WebService1.asmx/GetData" Json="true">
<ActionMethods Read="POST" />
<Reader>
<ext:JsonReader Root="d.data" TotalProperty="d.total" />
</Reader>
</ext:AjaxProxy>
</Proxy>
</ext:Store>
</Store>
<ColumnModel runat="server">
<Columns>
<ext:Column runat="server" Text="Test" DataIndex="test" />
<ext:DateColumn runat="server" Text="Date" DataIndex="date" />
</Columns>
</ColumnModel>
</ext:GridPanel>
</form>
</body>
</html>

WebService

using System;
using System.Web.Services;
using Ext.Net;

namespace Work2
{
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[System.Web.Script.Services.ScriptService]
public class WebService1 : System.Web.Services.WebService
{
[WebMethod]
public object GetData()
{
return new
{
data = JSON.Serialize(new object[]
{
new
{
test = "test1",
date = DateTime.Today
},
new
{
test = "test2",
date = DateTime.Today.AddDays(1)
}
}),
total = 2
};
}
}
}


This way this setting should affect.

Ext.Net.JSON.GlobalSettings.DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.Local

By the way, you could avoid such problems using, for example, an ASHX handler instead of a WebService.
http://localhost:51297/#/GridPanel/Paging_and_Sorting/Handler/

Is there a specific reason why you picked up a WebService?

supera
Jul 26, 2013, 5:44 PM
Daniil! Thanks a lot for your help!!! I think that expensive for you show me a trail! Thanks again!

No reason for webservices... I belived it was the right way to integrate client and the server...

Why I could avoid such problems using ASHX handler instead of a WebService?

Excuse me for being so stupid in this matter!

Daniil
Jul 26, 2013, 6:19 PM
No reason for webservices... I belived it was the right way to integrate client and the server...


Well, using WebServices is good to wire up server and client. Unless you run into such issues.



Why I could avoid such problems using ASHX handler instead of a WebService?


Well, an ASHX handler doesn't serialize the things automatically. You will be forced to serialized it manually. So, more room for flexibility. And JSON.Serialize() is nice to use for that.

supera
Jul 26, 2013, 6:33 PM
Ok!

I will follow the trail that you showed to me!

Thanks a lot, Daniil!!!

anup
Oct 31, 2013, 1:55 PM
No reason for webservices... I belived it was the right way to integrate client and the server...

Why I could avoid such problems using ASHX handler instead of a WebService?

Sorry I am very late to this thread. The other thing to bear in mind is that ASMX WebServices uses the DataContractJsonSerializer, not Json.NET. I remember stumbling onto this while writing the book. Here's what I wrote (sorry, not plugging the book, just remembered it was in there!)

p.147:

If we looked at the result, we would see something odd-looking, for example, /Date(1335752521448)/.

If the response from the server was a string, or a number, there'd be no problems here. Dates from ASMX Web Services, however, are a bit more involved.

Unfortunately, JSON doesn't specify a standard way to represent dates, while strings and numbers are fine. The JSON serialization used by ASMX services (DataContractJsonSerializer) creates a date using a pattern that was initially designed to be consumed by its own client-side AJAX library.

However, we are using Ext.NET and Ext JS, so we would have to handle this difficult-to-debug format manually. We can use Ext JS's client-side Date formatting API, for example, Ext.Date.parseDate("/Date(1335752521448)/", 'MS') which will give you back a JavaScript date object.

Alternatively, you could configure your service to return the date as a string that can be parsed when needed. Unfortunately, there is still no one way for JSON to agree and handle dates but at least there are some options here.

It is for reasons like this that I personally prefer to use ASHX or an MVC Controller for DirectMethods instead of ASMX, for example.

Hope that helps.

Daniil
Nov 04, 2013, 6:06 AM
Thank you for the valuable input, Anup. Much appreciated as always.

supera
Nov 04, 2013, 10:37 AM
Anup... thanks a lot for your post!

I solved my problem converting a date for a string in my objects before return this objects in my webservice... Just like you mentioned.

Works very fine.



Alternatively, you could configure your service to return the date as a string that can be parsed when needed. Unfortunately, there is still no one way for JSON to agree and handle dates but at least there are some options here.