Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hello sir,

i am binding gridview but my issue is :
i want to date with this format
<% #Eval("contractdate", "{0:dd/MM/yyyy}") %>

but i got date everytime MM/dd/yyyy format

i know that from where this issue comes
in stored procedure there is a convertion done for this "contractdate" as "MM/dd/yyyy"
but i cant modify this sp coz this sp is used at other pages in project
so my question is
can i convert date format in eval without changing sp ?
if yes then how ?
coz i search on google but not got any idea

thanx.
Posted

1 solution

Assuming you don't have any blank dates, and they're always formatted as "MM/dd/yyyy", this should work:
ASP.NET
<%# DateTime.ParseExact(Eval("contractdate", "{0}"), "MM/dd/yyyy", System.Globalization.CultureInfo.InvariantCulture).ToString("dd/MM/yyyy", System.Globalization.CultureInfo.CurrentCulture) %>

You could make this more resilient and tidy it up by using a function in your code-behind:
C#
using System.Globalization;
...
public static string ReformatDate(string valueFromDatabase)
{
    DateTime value;
    if (!DateTime.TryParseExact(valueFromDatabase, "MM/dd/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out value))
    {
        return string.Empty;
    }
    
    return value.ToString("dd/MM/yyyy", CultureInfo.CurrentCulture);
}

ASP.NET
<%# ReformatDate(Eval("contractdate", "{0}")) %>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900