Solved

Format decimal time into regular for display to user

Posted on 2011-09-23
7
229 Views
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

0
Comment
Question by:LessIsMore
  • 4
  • 3
7 Comments
 
LVL 7

Expert Comment

by:Slimfinger
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).)
0
 

Author Comment

by:LessIsMore
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
0
 
LVL 7

Accepted Solution

by:
Slimfinger 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(
   Year,
   Month,
   Day,
   (int)fTime,
   (int)((fTime - (int)fTime) * 60),
   0);

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

Open in new window

0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

Author Comment

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

Expert Comment

by:Slimfinger
ID: 36601851
Should be obvious which one is no good, huh?
0
 

Author Comment

by:LessIsMore
ID: 36709879
Not to me, no. Can you get off you horse and help me?
0
 
LVL 7

Expert Comment

by:Slimfinger
ID: 36712601
What year, month, and date did you provide?
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In this tutorial I will show you how to make a simple HTML bar chart with the usage of WhizBase, If you want more information about WhizBase please read my previous articles at http://www.experts-exchange.com/ARTH_5123186.html (http://www.experts-ex…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
The viewer will learn how to count occurrences of each item in an array.

708 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

17 Experts available now in Live!

Get 1:1 Help Now