Changing a datetime value using string manipulation

Category VB.Net Sub Category Manipulating Types
Bookmark and Share
By adding string values to dates you can alter the final date value as follows.
You want to work out a date to a specific time then add the time as a string part to the tostring part of the intended date.

Note the format used in the string conversion to make the date appear with a Jan Feb Mar month value. This avoids confusion when we deal with dd/mm (uk) or mm/dd (usa) formats.

This can then be converted back to a date value with CDate.

Copy Code
Dim TestDate As DateTime = CDate(String.Concat(Now.Date.ToString("dd MMM yyyy"), " 23:59:59")) 'result is 3/15/2010 11:59:59 PM# (was run on 15 Mar 2010) Dim StringDate as string = TestDate.ToString("dd MMM yyyy") 'result is "15 Mar 2010"

Share the love
Bookmark and Share