Results 1 to 4 of 4

Thread: Format Now() to "yyyymmddhhmmss" in eVB

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2006
    Posts
    73

    Format Now() to "yyyymmddhhmmss" in eVB

    Hi all,
    how do i format the current date Now() to the format of yyyymmddhhmmss in eVB? i know that in VB6 we can simple use

    format(Now(), "yyyymmddhhmmss"

    but in eVB, this cannot be done. can someone help me with the coding to get what i want? thks in advance

    longwar

  2. #2

    Thread Starter
    Lively Member
    Join Date
    Apr 2006
    Posts
    73

    Re: Format Now() to "yyyymmddhhmmss" in eVB

    anyone can help me in this?

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Apr 2006
    Posts
    73

    Re: Format Now() to "yyyymmddhhmmss" in eVB

    hi all,
    seem like i have to solve it myself. anyway, i have used this function to format the date to "yyyymmddhhmmss". anyone who have better way of doing it can let me know as well.

    'format date to "yyyymmddhhmmss"
    Public Function formatDateToString(Datetemp As Date) As String
    Dim strMth As String, strDay As String, strHr As String
    Dim strMin As String, strSec As String

    If Len(Month(Datetemp)) = 1 Then
    strMth = "0" & Month(Datetemp)
    Else
    strMth = Month(Datetemp)
    End If

    If Len(Day(Datetemp)) = 1 Then
    strDay = "0" & Day(Datetemp)
    Else
    strDay = Day(Datetemp)
    End If

    If Len(Hour(FormatDateTime(Datetemp, vbLongTime))) = 1 Then
    strHr = "0" & Hour(FormatDateTime(Datetemp, vbLongTime))
    Else
    strHr = Hour(FormatDateTime(Datetemp, vbLongTime))
    End If

    If Len(Minute(FormatDateTime(Datetemp, vbLongTime))) = 1 Then
    strMin = "0" & Minute(FormatDateTime(Datetemp, vbLongTime))
    Else
    strMin = Minute(FormatDateTime(Datetemp, vbLongTime))
    End If

    If Len(Second(FormatDateTime(Datetemp, vbLongTime))) = 1 Then
    strSec = "0" & Second(FormatDateTime(Datetemp, vbLongTime))
    Else
    strSec = Second(FormatDateTime(Datetemp, vbLongTime))
    End If

    Dim strTemp As String

    strTemp = Year(Datetemp) & strMth & strDay & strHr & strMin & strSec
    formatDateToString = strTemp
    End Function

  4. #4
    New Member
    Join Date
    Mar 2009
    Posts
    1

    Re: Format Now() to "yyyymmddhhmmss" in eVB

    I coded a function in eVB to format any date+time string: Parameters needed are the following
    Dato - DateTime to format
    FormatoF - # of format for the date part (see code below for detail)
    SeparadorF - string to separate day, month, year parts. Use "" for no separator
    FormatoH - # of format for the time part (see code below for detail)
    SeparadorH - string to separate hours, minutes, seconds. Use "" for no separator

    Function is in current use in aplications developed for mobile devices, so it has been tested a lot. Hope it is useful to you.

    As a developer in a spanish speaking country, documentation is in that lenguaje. Nowadays that language is a common one.

    Public Function FormatoFecha(ByVal Dato As DateTime, ByVal FormatoF As Integer, ByVal SeparadorF As String, FormatoH As Integer, ByVal SeparadorH As String) As String
    'Generar string de acuerdo al formato indicado con parámetros (FormatoF, FormatoH)
    'usando los separadores indicados para fecha (SeparadorF) y hora (SeparadorH)
    'Dato: valor para generar el string
    'FormatoF: para la parte de fecha del parámetro Dato, * denota el separador (omitible con "")
    ' 0 - No incluir dato de fecha (exclude date part)
    ' 1 - "DD*MM*YYYY"
    ' 2 - "DD*MM*YY"
    ' 3 - "MM*DD*YYYY"
    ' 4 - "MM*DD*YY"
    ' 5 - "YYYY*MM*DD"
    'FormatoH: para la parte de hora del parámetro Dato, * denota el separador (omitible con "")
    ' 0 - No incluir dato de hora (exclude time part)
    ' 1 - "HH*MM*SS" HH en el rango [0,24]
    ' 2 - "HH*MM" HH en el rango [0,24]
    ' 3 - "HH*MM*SS NN" HH en el rango [0,12] NN indica AM o PM
    ' 4 - "HH*MM NN" HH en el rango [0,12] NN indica AM o PM
    'SeparadorF: caracter para separar entre día, mes y año, ejemplos: "/" "-" "\"
    'SeparadorH: caracter para separar entre hora, minutos, segundos, ejemplos: ":", "-", "+"
    'NOTAS: "" en SeparadorF o SeparadorH implica que no se incluye caracter separador
    ' Si día, mes, hora, minuto o segundo es menor a 10 se antepone 0 para tener longitud= 2
    'Retorno: Fecha y/u hora según formatos, separados con espacio en blanco si se incluyen ambas.

    Dim Dia As String, Mes As String, Anio As String
    Dim DiaN As Integer, MesN As Integer, AnioN As Integer
    Dim Hora As String, Min As String, Seg As String
    Dim HoraN As Integer, MinN As Integer, SegN As Integer
    Dim FechaF As String, HoraF As String, AMPM As String

    'Si se excluyen ambos datos o no se recibe dato: terminar proceso
    If (FormatoF = 0 And FormatoH = 0) Or IsNull(Dato) Or Dato = "" Then
    FormatoFecha = ""
    Exit Function
    End If

    'Obtener valores separados:
    DiaN = Day(Dato)
    MesN = Month(Dato)
    AnioN = Year(Dato)
    HoraN = Hour(Dato)
    MinN = Minute(Dato)
    SegN = Second(Dato)

    'Pasar a variables tipo string:
    Dia = PadLeft(CStr(DiaN), 2, "0")
    Mes = PadLeft(CStr(MesN), 2, "0")
    Anio = PadLeft(CStr(AnioN), 4, "0")
    Hora = PadLeft(CStr(HoraN), 2, "0")
    Min = PadLeft(CStr(MinN), 2, "0")
    Seg = PadLeft(CStr(SegN), 2, "0")

    'Cambiar hora, incluir AM o PM según formato indicado:
    AMPM = ""
    If FormatoH = 3 Or FormatoH = 4 Then
    AMPM = " AM"
    If HoraN > 12 Then 'Si es hora PM:
    Hora = CStr(HoraN - 12)
    If HoraN - 12 < 10 Then Hora = "0" & Hora 'Anteponer 0 si corresponde:
    AMPM = " PM"
    End If
    End If

    'Concatenar datos de fecha según formato indicado:
    Select Case FormatoF
    Case 0
    FechaF = ""
    Case 1 '"DD*MM*YYYY"
    FechaF = Dia & SeparadorF & Mes & SeparadorF & Anio
    Case 2 '"DD*MM*YY"
    FechaF = Dia & SeparadorF & Mes & SeparadorF & Mid(Anio, 3, 2)
    Case 3 '"MM*DD*YYYY"
    FechaF = Mes & SeparadorF & Dia & SeparadorF & Anio
    Case 4 '"MM*DD*YY"
    FechaF = Mes & SeparadorF & Dia & SeparadorF & Mid(Anio, 3, 2)
    Case 5 '"YYYY*MM*DD"
    FechaF = Anio & SeparadorF & Mes & SeparadorF & Dia
    Case Else 'Usar formato YYYY*MM*DD
    FechaF = Anio & SeparadorF & Mes & SeparadorF & Dia
    End Select

    'Concatenar datos de hora según formato indicado:
    Select Case FormatoH
    Case 0
    HoraF = ""
    Case 1 '"HH*MM*SS"
    HoraF = Hora & SeparadorH & Min & SeparadorH & Seg
    Case 2 '"HH*MM"
    HoraF = Hora & SeparadorH & Min
    Case 3 '"HH*MM*SS NN"
    HoraF = Hora & SeparadorH & Min & SeparadorH & Seg & AMPM
    Case 4 '"HH*MM NN"
    HoraF = Hora & SeparadorH & Min & AMPM
    Case Else 'Usar formato "HH*MM NN"
    HoraF = Hora & SeparadorH & Min & AMPM
    End Select

    'Devolver resultado:
    FormatoFecha = Trim(FechaF & " " & HoraF)
    End Function

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width