Wednesday, 19 March 2014

Get CurrentCulture DateTime in Asp.net or C#


 To get current culture date please use following c# code.Based on country code we can able to get current datetime
 
// Persist the date and time data.
            StreamWriter sw = new StreamWriter(@".\DateData.dat");
 
            // Create a DateTime value.      
            DateTime dtIn = DateTime.Now;
            // Retrieve a CultureInfo object.
            CultureInfo invC = CultureInfo.InvariantCulture;
 
            // Convert the date to a string and write it to a file.
            sw.WriteLine(dtIn.ToString("r", invC));
            sw.Close();
 
            // Restore the date and time data.
            StreamReader sr = new StreamReader(@".\DateData.dat");
            String input;
            while ((input = sr.ReadLine()) != null)
            {
                Console.WriteLine("Stored data: {0}\n", input);
 
                // Parse the stored string.
                DateTime dtOut = 
DateTime.Parse(input, invC, DateTimeStyles.RoundtripKind);
 
                // Create a French (France) CultureInfo object.
                CultureInfo frFr = new CultureInfo("fr-FR");
                // Displays the date formatted for the "fr-FR" culture.
                Console.WriteLine
("Date formatted for the {0} culture: {1}",
                                  frFr.Name, dtOut.ToString("f", frFr));
                // Creates a German (Germany) CultureInfo object.
                CultureInfo deDe = new CultureInfo("de-De");
                // Displays the date formatted for the "de-DE" culture.
                Console.WriteLine("Date formatted for {0} culture: {1}",
                                  deDe.Name, dtOut.ToString("f", deDe));
            }
            sr.Close();
 
Get CurrentCulture DateTime in Asp.net or C# 

1 comment:

  1. so nice article and useful to Dot Net learners. we are also providing Dot NEt online training our Cubtraining global leader in providing in Dot Net course.

    ReplyDelete