Use this space to put some text. Update this text in HTML

468x60 banner ad

Advertise with Us

Powered by Blogger.
Showing posts with label Custom Datetime String Format. Show all posts
Showing posts with label Custom Datetime String Format. Show all posts

Friday 11 March 2016

Custom Datetime String Format

We need to show the date 07/03/2016 as 7th March, 2016.
Custom  Date Format like 7th March, 2016.
we want to denote the number position in an ordered sequence such as 1st, 2nd, 3rd, 4th...etc.

 Create a ASPX page and name it custom-datetime-string-format.aspx, in the page we take a Label Control and name it to lblInvoiceDateData.

Example:
custom-datetime-string-format.aspx Page



<form id="form1" runat="server">
<table>
<tr>
  <td width="100%" valign="top" align="right">
     <span style="font-family: Calibri; font-size: 18px; font-weight: bold;">
       DATE <asp:Label ID="lblInvoiceDateData" runat="server"></asp:Label>
     </span>
  </td>
</tr>
</table>
</form>

custom-datetime-string-format.aspx.cs Page



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

public partial class Test_custom_datetime_string_format : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
          
            ViewState["Invoice_ID"] = 2;

            //Master Details
            FillForm();
        }
    }
    //Fill Previous data for update
    private void FillForm()
    {
        clsCustomDatetime objInv = new clsCustomDatetime();
        DataSet ds = new DataSet();

        try
        {
            objInv.Invoice_ID = Convert.ToInt32(ViewState["Invoice_ID"]);
            ds = objInv.InvoiceReport();

            if (ds.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    //need to show the date 03/03/2012 as March 3rd,2012 etc
                    //lblInvoiceDateData.Text = dr["Invoice_Date"].ToString();
                    DateTime date = Convert.ToDateTime(dr["Invoice_Date"].ToString());
                    var formattedDate = string.Format(new MyCustomDateProvider(), "{0}", date);
                    lblInvoiceDateData.Text = formattedDate;

               }
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            ds = null;
            objInv = null;
        }
    }
    #region Start date custom format 07/03/2012 to  7th March,2012 in c#
    public class MyCustomDateProvider : IFormatProvider, ICustomFormatter
    {
        public object GetFormat(Type formatType)
        {
            if (formatType == typeof(ICustomFormatter))
                return this;

            return null;
        }

        public string Format(string format, object arg, IFormatProvider formatProvider)
        {
            if (!(arg is DateTime)) throw new NotSupportedException();

            var dt = (DateTime)arg;

            string suffix;

            if (new[] { 11, 12, 13 }.Contains(dt.Day))
            {
                suffix = "th";
            }
            else if (dt.Day % 10 == 1)
            {
                suffix = "st";
            }
            else if (dt.Day % 10 == 2)
            {
                suffix = "nd";
            }
            else if (dt.Day % 10 == 3)
            {
                suffix = "rd";
            }
            else
            {
                suffix = "th";
            }

            return string.Format("{1}{2} {0:MMMM}, {0:yyyy}", arg, dt.Day, suffix);
        }
    }
    #endregion End date custom format 07/03/2012 to 7th March,2012 in c#
}

Result

DATE 7th March, 2016