Posts

Showing posts from March, 2014

Get CurrentCulture DateTime in Asp.net or C#

Image
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 = newStreamWriter(@".\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 = newStreamReader(@".\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 o…

Calculate Days,Month and Year from To and From Dates

const double daysToMonths = 30.4368499;

                if (dtOne != null &&  dtTwo != null)
                {
                    if (dtTwo.Value > dtOne.Value)
                    {
                        double days = (dtTwo.Value - dtOne.Value).TotalDays;                       
                        double months = Math.Round(days / daysToMonths);                       
                        double years = dtTwo.Value.Year - dtOne.Value.Year;
            }

Universal Currency Format in C#

public static string FormatCurrency(object currencyValue)
        {
            CultureInfo culture = CultureInfo.CreateSpecificCulture("en-US");
            culture.NumberFormat.CurrencyNegativePattern = 1;
            return string.Format(culture, "{0:C}", currencyValue);
        }