我在应用程序上有一个非常奇怪的行为。服务器在纽约(-5 GMT 时区)和客户端 - 我,在罗马尼亚(+2 GMT 时区),所以有 7 小时的差异。我面临的问题是当我尝试保存日期时,比如说 12:00(日期无关紧要),客户端发送日期为 12:00 的请求,12:00 到达数据库但是当它返回服务器返回 19:00 小时。我试图在本地调试以查看谁弄乱了日期,但由于我在服务器和客户端上的日期相同,因此没有差异。
这是发送到服务器的参数 &startDate=07/25/2012%2012:00:00
这就是结果:1343232000000 - 从纪元开始的秒数(如果你使用转换器 - http://www.epochconverter.com/ - 你会看到日期实际上是 2012 年 7 月 25 日星期三 19:00:00
这里有一些代码片段:
public static void GetProfessionalsHours(List<long> ids, out List<SalonProfessional> professionals)
{
professionals = new List<SalonProfessional>();
using (SqlConnection conn = new SqlConnection(DbConfig.ConnectionString))
{
using (
SqlCommand command = new SqlCommand("GetProfessionalsHours", conn) { CommandType = CommandType.StoredProcedure })
{
conn.Open();
command.Parameters.AddWithValue("professionalIDs", ids.CommaSeparated());
using (IDataReader reader = command.ExecuteReader())
{
//get normal schedule
while (reader.Read())
{
professionals.Add(SalonProfessional.GetSalonProfessional(reader));
}
reader.NextResult();
while (reader.Read())
{
professionals.Find(p => p.ID == reader.GetInt64(1)).Hours.Add(ProfessionalHours.GetProfessionalHour(reader));
}
//get overriden hours
reader.NextResult();
while (reader.Read())
{
professionals.Find(p => p.ID == reader.GetInt64(1)).OverriddenHours.Add(ProfessionalOverriddenHour.GetProfessionalOverriddenHour(reader));
}
}
}
}
}
public static ProfessionalOverriddenHour GetProfessionalOverriddenHour(IDataReader reader)
{
return new ProfessionalOverriddenHour()
{
ID = reader.GetInt64(0),
ProfessionalId = reader.GetInt64(1),
StartDate = reader.GetDateTime(2),
EndDate = reader.GetDateTime(3),
};
}
public JsonResult CalendarData(List<long> professionalIDs, CalendarData calendarData)
{
AjaxResponse response = new AjaxResponse();
response.Success = true;
CalendarDataResponseObject responseData = new CalendarDataResponseObject();
response.Content = responseData;
try
{
List<SalonProfessional> professionals = null;
CalendarOperations.GetProfessionalsHours(professionalIDs, out professionals);
responseData.Professionals = professionals;
}
catch (Exception ex)
{
response.Success = true;
response.ErrorMessage = "Could not retrieve calendar data";
ExceptionsOperations.LogException(ex, "Could not retrieve calendar data");
}
return Json(response, JsonRequestBehavior.AllowGet);
}
问题出在 ProfessionalOverriddenHour 对象的 StartDate 和 EndDate 上。