-2
{"meritSystemCalendar":{"businessDate":"2021-04-21T00: 00: 00","cycleCode":"D"},"responseCode":"ok","statusCode":200}

On parsing the BusinessdDate into a DateTime property, the value stored is "21-04-2021 00:00:00"

How to convert the property to "4/21/2020" in c#

5
  • 5
    Does this answer your question? Specifying a custom DateTime format when serializing with Json.Net Commented Apr 22, 2021 at 6:33
  • 2
    What is the real problem> The value is not stored as "21-04-2021 00:00:00". Datetime has no format, it's a binary value just like byte or int. It actually stores an Int64 tick count internally. Formats only apply when parsing a string into a DataTime, or formatting a DateTime into a string for display Commented Apr 22, 2021 at 6:39
  • 2
    I didn't downvote but there must be at least 3 identical questions each week. A simple search would find them. Commented Apr 22, 2021 at 6:41
  • 1
    Does this answer your question? Getting Date or Time only from a DateTime Object Commented Apr 22, 2021 at 7:13
  • When you convert a Strng to datetime the format will depend on the settings in your machine. There is nothing wrong. If you want a different format than you have to change the settings in VS to display the DateTime to a different culture. Commented Apr 22, 2021 at 7:16

1 Answer 1

0

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.