convert string to datetime vb.net

前端 未结 4 1031
北海茫月
北海茫月 2021-01-24 14:15

i need to convert strings to date format. the requirement is if current month is selected, the date should be getdate. if any other month is selected then it should be first of

4条回答
  •  孤街浪徒
    2021-01-24 14:59

    DateTime.ParseExact Method (String, String, IFormatProvider)

    DateTime dt = DateTime.ParseExact(dateString,formatString);
    dt = (dt.Month == DateTime.Now.Month) ? DateTime.Now : dt;
    

    If your calling this alot in a loop performance may be better if you only call DateTime.Now once and store it in a variable before comparison, since DateTime.Now is a fairly expensive operation.

    If the SQL server column is of type DateTime then you don't need to worry about the format, jut pass the DateTime object and it will work.

提交回复
热议问题