date fomatting struggles

Posted on 2006-05-04
Last Modified: 2010-04-23

I am having trouble formatting my date and time so that it can be sent to a mysql database

the table has column
receiving_date as type Date
From MySQL Manual
A date. The supported range is '1000-01-01' to '9999-12-31'. MySQL displays DATE values in 'YYYY-MM-DD' format, but allows you to assign values to DATE columns using either strings or numbers.

receiving_time as type Time  
A time. The range is '-838:59:59' to '838:59:59'. MySQL displays TIME values in 'HH:MM:SS' format, but allows you to assign values to TIME columns using either strings or numbers.

My function is this

Public Function getTime() As DateTime
        Dim currentTime As DateTime

        'get system time

        currentTime = DateTime.Now

        Return currentTime
    End Function

So now I need to format the date so I get YYYY-MM-DD and time to be HH:MM:SS

I was going to individually parse the date and format a new string into those two formats but I was hoping there was a better way of formatting it right in the function and returning the necessary info cuz I use this function in a couple places

Question by:foobarr
    LVL 3

    Accepted Solution

    Do you mean:
    Dim currentTime As DateTime
    currentTime = DateTime.Now"yyyy-MM-dd"))"hh:mm:ss"))
    LVL 7

    Assisted Solution

    Just a note to maidinhtai's comment:

    Note that the format strings are case senstive. So this won't work properly:


    you should get 2006/00/4 for today. So make sure you use "MM" and not "mm"

    That drove me crazy when I was researching it a while back. Here's a good sample from Microsoft.

    Good luck.
    LVL 7

    Author Comment

    wouldn't I need


    i will give it a try later on and post back

    LVL 7

    Expert Comment

    well not necessarily. When I say case sensitive, I mean that I know MM and mm do not return the same results.
    LVL 7

    Author Comment

    Dim currentTime As Date

            Dim currentDate As Date

            currentTime = Date.Now
            currentDate = Date.Now

            MessageBox.Show("Current Date: " & currentDate.ToString("yyyy-MM-dd"))
            MessageBox.Show("Current Time: " & currentTime.ToString("HH:MM"))

    this seemed to work
    LVL 7

    Author Comment

    actualy the last one was"CurrentTime: " & currentTime.toString("HH:mm"))

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
    1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now