locked
Issue in converting date in european and US format in globalization RRS feed

  • Question

  • User-715140818 posted

    I am working on globalization project.In some scenario I want to convert date in european format and sometimes in US format.

    I have developed method as follow.

    public static string ToCultureDate(object oValue,string culture)

    {

     System.Threading.Thread.CurrentThread.CurrentCulture = cultureinfo; 

     CultureInfo cultureinfo = CultureInfo.CreateSpecificCulture(culture);

     DateTime date = Convert.ToDateTime(oValue); 

     return date.ToString();

     }

    In this method I try to pass date as object and culture as en-US or fr-FR and accordingly I should get short date format.

    But I am getting error Unable to cast object of type 'System.Web.UI.WebControls.Label' to type 'System.IConvertible'.
    Actually I am passing object as date and that date is stored in label.

    Above method is called as under



    lblPickupdate.Text=ToCultureDate(lblPickupdate,languageCulture);

    where lblPickupdate is label which holds date.

     

     

    Wednesday, October 6, 2010 10:27 PM

Answers

  • User1224194097 posted

    Create a Extension method for date conversion like this

    public static class DateHelper
    {
        public static string ToCultureDate(this string oValue, string culture)
        {
            return DateTime.Parse(oValue, new CultureInfo(culture)).ToString();
        }
    }

    Add that class to current project and use it like this

    lblPickupdate.Text = lblPickupdate.Text.ToCultureDate("fr-FR");
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 7, 2010 1:37 AM

All replies

  • User1224194097 posted

    Create a Extension method for date conversion like this

    public static class DateHelper
    {
        public static string ToCultureDate(this string oValue, string culture)
        {
            return DateTime.Parse(oValue, new CultureInfo(culture)).ToString();
        }
    }

    Add that class to current project and use it like this

    lblPickupdate.Text = lblPickupdate.Text.ToCultureDate("fr-FR");
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 7, 2010 1:37 AM
  • User-715140818 posted

    I am getting error {"String was not recognized as a valid DateTime."} 

    when I debbuged it it shows oValue as 22/09/2010 11:00:00 and culture as "en-US"

    Here is my method

    public class Helper

    {

    public static string ToCultureDate(string oValue,string culture)

        {

            CultureInfo cultureinfo = CultureInfo.CreateSpecificCulture(culture);

            System.Threading.Thread.CurrentThread.CurrentCulture = cultureinfo;

     

            return DateTime.Parse(oValue, new CultureInfo(culture)).ToString();

     

            }

    }

    And I am calling method as like as follow.

    lblPickupdate.Text =

    Helper.ToCultureDate(lblPickupdate.Text, languageCulture);

     

    Thursday, October 7, 2010 10:32 AM