I'm saving to a text file a serialized object that only contains fields of type DateTime, but the date is saving wrong.
I'll demonstrate with an example what I'm doing.
My DataBaseViewModel object contains two fields of type DateTime
[Serializable]
public class DataBaseViewModel
{
public DateTime ultimaAtualizacaoTfs { get; set; }
public DateTime ultimaAtualizacaoSis { get; set; }
}
Adding values to my object
var datasBase = new DatasBaseViewModel();
datasBase.ultimaAtualizacaoSis = new DateTime(2015, 11, 9, 15, 10, 10);
datasBase.ultimaAtualizacaoTfs = new DateTime(2015, 11, 9, 15, 11, 11);
Then save the serialized object into text
using (Stream stream = File.Open(_pathToDatasBase, FileMode.Create))
using (StreamWriter streamWriter = new StreamWriter(stream))
{
streamWriter.WriteLine(new JavaScriptSerializer().Serialize(dataBase));
}
At another time, I'll read the contents of my text file.
var datasBase = new DataBaseViewModel();
using (Stream stream = File.Open(_pathToDatasBase, FileMode.Open))
using (StreamReader streamReader = new StreamReader(stream))
{
datasBase = new JavaScriptSerializer().Deserialize<DataBaseViewModel>(streamReader.ReadToEnd());
}
After reading and deserializing, my object is with the wrong date, different from what I saved.
In this case, I saved the dates 09/11/2015 15:10:10
and 09/11/2015 15:11:11
and in the deserialized object I have dates 09/11/2015 17:10:10
and 09/11/2015 17:11:11
.
The text file with the serialized object is saved in this way:
{"ultimaAtualizacaoTfs":"\/Date(1447089071000)\/","ultimaAtualizacaoSis":"\/Date(1447089010000)\/"}