Format decimal time into regular for display to user

Posted on 2011-09-23
Last Modified: 2012-05-12
I have a formview to display users input.
When user enters time I have a dropdown menu where text is 1:30 pm but the value is 13:50. I need it for calculation of pay.
Later I need to display to user his input. My script to format decimal time to regular is attached. The problem is that I get displayed AM to PM time. Example 1:30 AM instead of 1:30 PM.
I realise that I've asked for it when wrote

        If hours >= 12 Then
            a = "PM"
        End If
but cannot figure out how to fix it
Public Function ToDateTime(ByVal value As String) As String
        Dim a As String = "AM"

        Dim tsplit As String() = value.Split("."c)

        Dim hours As Integer = Convert.ToInt32(tsplit(0))
        Dim minutes As Double = Convert.ToDouble(tsplit(1))

        If hours > 12 AndAlso hours <= 24 Then
            hours = hours - 12
        End If
        If hours = 0 Then
            a = "0"
        End If

        If hours >= 12 Then
            a = "PM"
        End If

        If minutes <= 99 Then
            minutes = (60 * (minutes / 100))
            minutes = Math.Floor(minutes)
        End If

        Dim h As String = hours.ToString()
        Dim m As String = minutes.ToString()

        Dim c As String = h & ":" & m & a

        Return c

    End Function

Open in new window

Question by:LessIsMore
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3

Expert Comment

by:Bill Nolan
ID: 36589162
Use the DateTime structure and its formatting methods.  (Create it with decimal values, output your desired format with one of the provided format functions, or ToString(string format).)

Author Comment

ID: 36589594
I looked into DateTime structure and cannot figure out how to exactly to do that. Can you help me out on example? I have 13.75 in database, how to make it look like 1:45PM?
I have problem only with PM, everything else is working

Accepted Solution

Bill Nolan earned 250 total points
ID: 36589734
See code.  You might want to play around with the format functions for output, or different constructors.
decimal fTime = 13.75m;

DateTime time = new DateTime(
   (int)((fTime - (int)fTime) * 60),

time.ToString("hh:mm tt");

Open in new window

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 36601788
I have an error message on it
System.ArgumentOutOfRangeException: Year, Month, and Day parameters describe an un-representable DateTime.

Expert Comment

by:Bill Nolan
ID: 36601851
Should be obvious which one is no good, huh?

Author Comment

ID: 36709879
Not to me, no. Can you get off you horse and help me?

Expert Comment

by:Bill Nolan
ID: 36712601
What year, month, and date did you provide?

Featured Post

Major Incident Management Communications

Major incidents and IT service outages cost companies millions. Often the solution to minimizing damage is automated communication. Find out more in our Major Incident Management Communications infographic.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
In threads here at EE, each comment has a unique Identifier (ID). It is easy to get the full path for an ID via the right-click context menu. However, we often want to post a short link within a thread rather than the full link. This article shows a…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

739 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