-1

I am passing "/Date(1541116800000)/" as string in my method. How to extract only date and then return it as string in if condition?

public convertDateToString(string value)
{
   if (DateTime.TryParseExact("yyyy-MM-dd"), out string dateValue)
        return value; (returns just the date from datetime in string format)
}
Kelvin Lawrence
  • 14,674
  • 2
  • 16
  • 38
Eva
  • 109
  • 9
  • 1
    Step one is to parse the string as `DateTime`. See marked duplicate. From that, it's a simple matter of then using `ToString()` on the resulting value with the appropriate format. See the hundreds of similar questions already on Stack Overflow for more details about that. – Peter Duniho Mar 18 '20 at 21:42

1 Answers1

2

It's a Javascript date. If you have a Json library like Newtonsoft.Json, try this:

public string convertDateToString(string value)
{
    var dt = JsonConvert.DeserializeObject<DateTime>($"\"{value}\"");
    return dt.ToString("yyyy-MM-dd");
}

Usage

convertDateToString("/Date(1541116800000)/")

Output

2018-11-02
weichch
  • 9,306
  • 1
  • 13
  • 25