1

I have a string value of 1 Jan which indicates a date for batch program to run based on the date.

May I know if there is quick method to convert 1 Jan to a DateTime value which I can use to compare if it is today?

Otherwise, I guess substring and replace of characters may be the only alternative I can think of.

Thank you

gymcode
  • 4,431
  • 15
  • 72
  • 128

2 Answers2

3

try ParseExact

var mydate = DateTime.ParseExact("1 Jan 2020", "d MMM yyyy", CultureInfo.InvariantCulture);

OR

var mydate = DateTime.ParseExact("1 Jan", "d MMM", CultureInfo.InvariantCulture);
Krishna Varma
  • 4,238
  • 2
  • 10
  • 25
0
string date = "1 Jan";
CultureInfo culture = new CultureInfo("en-US");
DateTime tempDate = Convert.ToDateTime(date, culture);
ElConrado
  • 1,477
  • 4
  • 20
  • 46
  • 2
    While this code snippet may be the solution, [including an explanation](//meta.stackexchange.com/questions/114762/explaining-entirely-‌​code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – f.khantsis Mar 30 '20 at 15:41