There are multiple methods to extract the date from the DateTime variable.
First:
You can use the [DataType(DataType.Date)]
annotation above the DateTime property in your model.
Second:
You can use ToString() method to capture the date from the DateTime variable.
The code snippet for the second method is below.
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
namespace ExportDTtoJSON
{
class Program
{
static void Main(string[] args)
{
List<Person> p = new List<Person>();
p.Add(new Person
{
FirstName = "Ahmad",
DOB = new DateTime(2021, 1, 1).ToString("yyyy/mm/dd")
});
p.Add(new Person
{
FirstName = "Khan",
DOB = new DateTime(2022, 3, 4).ToString("yyyy/mm/dd")
});
var jsonObject = Newtonsoft.Json.JsonConvert.SerializeObject(p);
}
}
public class Person
{
public string FirstName { get; set; }
public string DOB { get; set; }
}
}
jsonObject
has the JSON data, and you can apply your logic to that.