is there any way to convert date read from JSON file? Exemplary record from my file is:
{
"UserID": 73274,
"Created": "17-03-2018 06:35",
"Office": "Washington"
},
What I am doing is importing JSON file and save its content to database. The problem is, when date is in DD-MM-YYYY hh:ss
format, it simply doesn't work. But when date is in YYYY-MM-DDThh:mm:ss
format, everything is fine. How to parse date to another format after reading this file and execute following operations on file with parsed date?
Part of my controller responsible for this operations is:
if (file.ContentType == "application/json")
{
try
{
using (var reader = new StreamReader(file.OpenReadStream()))
{
content = reader.ReadToEnd();
}
}
catch
{
return BadRequest(Messages.fileNotChosen);
}
try
{
userObjects = JsonConvert.DeserializeObject<List<UserImportModel>>(content);
}
catch
{
return BadRequest();
}
}
When date format is YYYY-MM-DDThh:mm:ss
, userObjects is properly counted and controller moves to part where I execute adding to database. When date format is DD-MM-YYYY hh:ss
, userObjects = 0 and controller moves to catch, which then returns BadRequest().
EDIT: Code of UserImportModel
class:
public class UserImportModel
{
public string UserID { get; set; }
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}")]
public DateTime Created { get; set; }
public string Office { get; set; }
}