Tuesday, 5 August 2014

C# Conversion of DateTime to 24 Hours Time in Asp.Net | Display 24 Hour Time In DropDownList In Asp.net

8/05/2014 - By Pranav Singh 0

This article will show you how you can generate the list of 24 hour date time collection using c#.net and display the detail into dropdown list.


So for his article first we will create a new asp.net application and add a dropdown list into it. After adding dropdown list your .aspx page will look as shown below.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="24hrtime.aspx.cs" Inherits="ProjectDemo_Asp.et._24hrtime" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>C# Conversion of DateTime to 24 Hours Time in Asp.Net</title>
</head>
<body>
    <form id="form1" runat="server">
    24 Hours Time :<asp:DropDownList ID="DropDownList1" runat="server">
    </asp:DropDownList>
    <div>
   
    </div>
    </form>
</body>
</html>

Now check the code for getting 24 hour time and making collection.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ProjectDemo_Asp.et
{
    public partial class _24hrtime : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            BindTimeToDDL();
        }
        public void BindTimeToDDL()
        {
            List<string> time24hr = new List<string>();
            DateTime myDate = DateTime.ParseExact("2009-05-08 00:00", "yyyy-MM-dd HH:mm", System.Globalization.CultureInfo.InvariantCulture);
            for (int i = 0; i < 24; i++)
            {
                string strtime = FormatearHoraA24(myDate.AddHours(i));
                time24hr.Add(strtime);
            }
            DropDownList1.DataSource = time24hr.ToList();
            DropDownList1.DataBind();
        }
        public static string FormatearHoraA24(DateTime fechaHora)
        {
            return fechaHora.ToString("HH:mm");
        }
    }
}

In above code I am passing a dummy date with 00:00:00 time and retrieving time from the incremented time by hour.



 DOWNLOAD

Tags: ,
About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

0 comments:

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Global Classified : Connectseekers.com
© 2014 aspdotnet-pools.com Designed by Bloggertheme9.
back to top