Mysql TimeStamp converter class

This is a sample code in VB.NET for a class to take the date given as a date type or string type convert it into yyyymmddhhmmss string for use in MYSQL SQL strings.

Usage:
Dim dateConvert As New mySQLlDate

myDateString=dateConvert.convertDate(dateString) or
myDateString=dateConvert.convertDate(dateTime) or
myDateString=dateConvert.todaysDate



Public Class mySQLlDate
'
'class to convert dates to mysql yyyymmddhhmmss strings, for use in your SQL's
Public Sub New()
End Sub
'convertDate is an overloaded method that will handle string type dates and date type dates
'take the date given as a date type(from textboxes and convert into yyyymmddhhmmss string)
Public Function convertDate(ByVal input As Date)
Dim output As String

Dim hour As String = CStr(input.Hour)
Dim minute As String = CStr(input.Minute)
Dim second As String = CStr(input.Second)
Dim day As String = CStr(input.Day)
Dim month As String = CStr(input.Month)

If Len(CStr(input.Hour)) = 1 Then
hour = "0" & input.Hour
ElseIf Len(CStr(input.Hour)) = 0 Then
hour = "00"
End If

If Len(CStr(input.Minute)) = 1 Then
minute = "0" & input.Minute
ElseIf Len(CStr(input.Minute)) = 0 Then
minute = "00"
End If

If Len(CStr(input.Second)) = 1 Then
second = "0" & input.Second
ElseIf Len(CStr(input.Second)) = 0 Then
second = "00"
End If

If Len(CStr(input.Day)) = 1 Then
day = "0" & input.Day
ElseIf Len(CStr(input.Day)) = 0 Then
day = "00"
End If

If Len(CStr(input.Month)) = 1 Then
month = "0" & input.Month
ElseIf Len(CStr(input.Month)) = 0 Then
month = "00"
End If

convertDate = CStr(input.Year & month & day & hour & minute & second)
End Function
'take the date given as a string (from textboxes and convert into yyyymmddhhmmss string)
Public Function convertDate(ByVal dateString As String)
Dim input As Date

Dim conv As Convert
input = conv.ToDateTime(dateString)

Dim hour As String = CStr(input.Hour)
Dim minute As String = CStr(input.Minute)
Dim second As String = CStr(input.Second)
Dim day As String = CStr(input.Day)
Dim month As String = CStr(input.Month)

If Len(CStr(input.Hour)) = 1 Then
hour = "0" & input.Hour
ElseIf Len(CStr(input.Hour)) = 0 Then
hour = "00"
End If

If Len(CStr(input.Minute)) = 1 Then
minute = "0" & input.Minute
ElseIf Len(CStr(input.Minute)) = 0 Then
minute = "00"
End If

If Len(CStr(input.Second)) = 1 Then
second = "0" & input.Second
ElseIf Len(CStr(input.Second)) = 0 Then
second = "00"
End If

If Len(CStr(input.Day)) = 1 Then
day = "0" & input.Day
ElseIf Len(CStr(input.Day)) = 0 Then
day = "00"
End If

If Len(CStr(input.Month)) = 1 Then
month = "0" & input.Month
ElseIf Len(CStr(input.Month)) = 0 Then
month = "00"
End If

convertDate = CStr(input.Year & month & day & hour & minute & second)
End Function
'today's date as a string (from textboxes and convert into yyyymmddhhmmss string)
Public Function todaysDate()
Dim Input As Date = Date.Now

Dim hour As String = CStr(Input.Hour)
Dim minute As String = CStr(Input.Minute)
Dim second As String = CStr(Input.Second)
Dim day As String = CStr(Input.Day)
Dim month As String = CStr(Input.Month)

If Len(CStr(Input.Hour)) = 1 Then
hour = "0" & Input.Hour
ElseIf Len(CStr(Input.Hour)) = 0 Then
hour = "00"
End If

If Len(CStr(Input.Minute)) = 1 Then
minute = "0" & Input.Minute
ElseIf Len(CStr(Input.Minute)) = 0 Then
minute = "00"
End If


If Len(CStr(Input.Second)) = 1 Then
second = "0" & Input.Second
ElseIf Len(CStr(Input.Second)) = 0 Then
second = "00"
End If


If Len(CStr(Input.Day)) = 1 Then
day = "0" & Input.Day
ElseIf Len(CStr(Input.Day)) = 0 Then
day = "00"
End If

If Len(CStr(Input.Month)) = 1 Then
month = "0" & Input.Month
ElseIf Len(CStr(Input.Month)) = 0 Then
month = "00"
End If

todaysDate = CStr(Input.Year & month & day & hour & minute & second)
End Function
End Class




Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: