Abe
Abe

Reputation: 1929

datetime variable with extra hour and custom format as datetime not string

This is so simple but i am not able to figure out how to do it, please excuse my ignorance.

I would like a datetime variable to be current datetime + 1 hour in the format "yyyy-mm-dd HH:mm:ss"

I have tried this so far (dotnetfiddle.net) and when i assign it to the datetime variable the date is changed to slashes instead of hypen.

Imports System

    Public Module Module1
        Public Sub Main()
            Dim tet As String = Datetime.Now.AddHours(1).ToString("yyyy'-'MM'-'dd hh:mm:ss")
            Dim expiryTime As DateTime = DateTime.ParseExact("2009-05-08 14:40:52,531", "yyyy-MM-dd HH:mm:ss,fff",
                                           System.Globalization.CultureInfo.InvariantCulture)
            Console.WriteLine(convert.todatetime(tet))
            Console.WriteLine(tet)
            Console.WriteLine(Datetime.Now.AddHours(1))
        End Sub
    End Module

As per one of the comments I have used parseExact and i still see the date with slashes instead of hypen

screenshot

Upvotes: 1

Views: 292

Answers (1)

Mary
Mary

Reputation: 15081

When you convert tet back to a DateTime it will display as the default pattern. Keep it as a string for display. The convert it back with ParseExact when you need to use it as DateTime again.

Dim dNow As DateTime = DateTime.Now.AddHours(1)
Dim strNow As String = dNow.ToString("yyyy-MM-dd hh:mm:ss")
Console.WriteLine(strNow)

Upvotes: 1

Related Questions