简体   繁体   English

从文件读取JSON日期

[英]Read JSON date from file

I write my logs to a text file as JSON. 我将日志以JSON格式写入文本文件。 In the file the call obejct LogTime value is 在文件中,调用对象的LogTime值为

"1378289277591".
*{"LogTime":"Date(1378290565240)"}*

Consider the code below: 考虑下面的代码:

Public Class Sync{
  public async Task<CallModel> ConvertCallFileToCallObejct(string path)
        {
            try
            {
                using (var sr = new StreamReader(path))
                {
                    string callText = await sr.ReadToEndAsync();
                    var call = new JavaScriptSerializer().Deserialize<CallModel>(callText);

                    return call;
                }
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception.Message);
            }
        }
}

I convert the Call File to Call Object: 我将调用文件转换为调用对象:

var sync = new Sync();
CallModel call = sync.ConvertCallFileToCallObejct(e.FullPath).GetAwaiter().GetResult();

The problem is that Call.LogTime is 9/4/2013 10:29:25 AM but Using Chrome Console and new Date(1378290565240) the result is 9/4/2013 14:59:25 PM 问题是Call.LogTime9/4/2013 10:29:25 AM,但是使用Chrome控制台和新的Date(1378290565240)结果是9/4/2013 14:59:25 PM

What is the problem? 问题是什么?

try below code 尝试下面的代码

// JSON received from server is in string format
var jsonString = '{"date":1251877601000}';

//use JSON2 or some JS library to parse the string
var jsonObject =  JSON.parse( jsonString );

//now you have your date!
alert( new Date(jsonObject.date) );

我不确定您的时区是什么,但是我希望它的UTC日期是。

According to your profile, you live in Iran, where the timezone is UTC+3:30. 根据您的个人资料,您居住在时区为UTC + 3:30的伊朗。 However, in April, Iran uses daylight saving time so the real timezone is UTC+4:30. 但是,在4月,伊朗使用夏令时,因此实际时区为UTC + 4:30。

This means that UTC time of 9/4/2013 10:29:25 AM is 9/4/2013 14:59:25 PM local time in Iran. 这表示9/4/2013 10:29:25 AM UTC时间是伊朗当地时间9/4/2013 14:59:25 PM

According to ECMA specification , the time given in your JSON string is treated as UTC time, and it is deserialized as such. 根据ECMA规范 ,JSON字符串中给定的时间被视为UTC时间,并且将其反序列化。 You can check the return value of Call.LogTime expression which returns DateTimeKind.Utc . 您可以检查返回DateTimeKind.UtcCall.LogTime表达式的返回值。 Thus, what you see in your C# code is UTC time. 因此,您在C#代码中看到的就是UTC时间。

Now, Chrome also sees this time as UTC time, however it seems to display it as local time, according to your timezone. 现在,Chrome还将该时间视为UTC时间,但是根据您的时区,它似乎将其显示为本地时间。 I am not 100% sure, but I think that Chrome uses your list of preferred languages when choosing how to display date, so try to play with it - I have no idea what exactly it does, but I remember a similar problem when changing the language order affected how time was interpreted. 我不确定100%,但是我认为Chrome在选择日期显示方式时会使用您的首选语言列表,因此请尝试使用它-我不知道它的确切用途,但我记得在更改日期时遇到类似的问题语言顺序影响时间的解释方式。 OF course, it depends on what exactly you try to achieve - IMO, both values are correct, as it is the same time. 当然,这取决于您要达到的目标-IMO,这两个值是正确的,因为这是同时的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM