none
DateFormat Conversion Issues RRS feed

  • Question

  • static TimeZoneInfo tz = TimeZoneInfo.FindSystemTimeZoneById("India Standard Time");
    static DateTime UTCPingTime = TimeZoneInfo.ConvertTimeFromUtc(new DateTime(1970, 1, 1), tz);
    long dftDate = 1579583926000;
    
    DateTime dt = UTCPingTime.AddSeconds(dftDate);
    
    string dateTime = dt.ToString("dd-MMM-yyyy HH:mm:ss");
    
    Console.WriteLine(dateTime);

    Hi,

    The above code is working fine with long time format as HH:mm:ss in Control Panel-->Region Section (Windows 10).

    Suppose When we are changing the long format to h.mm.ss tt then the above code, format convertion is not working.

    Regards

    Prabu SP

    Tuesday, January 21, 2020 9:39 AM

All replies

  • To format a DateTime using the formatting rules configured by the user in Windows locale use the overload that accepts the `IFormatProvider`. To get the provider for the current UI use `CultureInfo.CurrentUICulture.DateTimeFormat`.

    var format = System.Globalization.CultureInfo.CurrentUICulture;
    var dt = DateTime.Now;
    
    var str = dt.ToString(format.DateTimeFormat);
    You can also use format specifiers to control whether short or long formats are used.


    Michael Taylor http://www.michaeltaylorp3.net

    Tuesday, January 21, 2020 2:49 PM
    Moderator
  • Hi SeenivasaPrabu,

    long dftDate = 1579583926000;

    when I run your code firstly, the error occured that"Value to add was out of range".
    It is caused by exceeding the scope of DateTime MaxValue.
    You can change the datetime format via coding insteading of setting in Region Section.

    string dateTime = dt.ToString("dd-MMM-yyyy h.mm.ss");

    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, January 22, 2020 9:44 AM
  • Hi SeenivasaPrabu,
    Has your problem been solved? If it is resolved, we suggest that you mark it as the answer. So it can help other people who have the same problem find a solution quickly.
    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, March 6, 2020 8:57 AM
  • hellow friend try the bellow code may give success

    using System.Globalization;


    dt.ToString("dd-MMM-yyyy H:mm:ss",CultureInfo.InvariantCulture);

    Friday, March 6, 2020 9:40 AM