locked
convert server time to local time RRS feed

  • Question

  • User-807418713 posted

    hello

    how to convert this using asp.net c#

     Server Time :- 07-JAN-2021 06:03:20

    Local Time :- 07-JAN-2021 18:34:00

    Thanking You

    Thursday, January 7, 2021 2:04 PM

Answers

  • User1535942433 posted

    Hi Gopi.MCA,

    Compiler Error Message: CS0246: The type or namespace name 'TimeZoneInfo' could not be found (are you missing a using directive or an assembly reference?)

    I'm guessing that your framework version is 2.0.And TimeZoneInfo only support more than 3.5.

    You could use ToLocalTime().But your server time need to be UTC.

    Just like this:

    Console.WriteLine("Enter a date and time in universal time.");
            strDateTime = Console.ReadLine();
    
            try {
                univDateTime = DateTime.Parse(strDateTime);
                localDateTime = univDateTime.ToLocalTime();
    
                Console.WriteLine("{0} universal time is {1} local time.",
                                         univDateTime,
                                         localDateTime);
            }
            catch (FormatException) {
                Console.WriteLine("Invalid format.");
                return;
            }

    More details,you could refer to below article:

    https://docs.microsoft.com/en-us/dotnet/api/system.datetime.tolocaltime?view=netframework-2.0

    Best regards,

    Yijing Sun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 20, 2021 9:26 AM

All replies

  • User-1545767719 posted

    Where and when do you want to get the times? Is it OK for you to get the times at server side on each request?

    Thursday, January 7, 2021 9:52 PM
  • User1535942433 posted

    Hi Gopi.MCA,

    You could do like this:

    string zoneId = "Central European Standard Time";
    TimeZoneInfo tzi = TimeZoneInfo.FindSystemTimeZoneById(zoneId);
    DateTime result = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow,tzi);
    Console.WriteLine("Time is " + result + " in Denmark");

    Using the TimeZoneInfo class is the only reliable way in .Net to convert to/from different timezones AND get proper DST conversions.

    TimeZoneInfo.ConvertTimeToUtc(dtLocal,tzi) is the reverse converting from a local time to a utc time.

    Best regards,

    Yijing Sun

    Friday, January 8, 2021 5:33 AM
  • User-807418713 posted

    Hello

    Getting Error Please Check any other method

    Server Error in '/TEST' Application.
    Compilation Error
    Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
    
    Compiler Error Message: CS0246: The type or namespace name 'TimeZoneInfo' could not be found (are you missing a using directive or an assembly reference?)
    
    Source Error:
    
    
    Line 22:         {
    Line 23:             string zoneId = "Central European Standard Time";
    Line 24: TimeZoneInfo tzi = TimeZoneInfo.FindSystemTimeZoneById(zoneId);
    Line 25: DateTime result = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow,tzi);
    Line 26: Response.Write(result);

    Thanking You

    Tuesday, January 19, 2021 7:28 PM
  • User-807418713 posted

    Hello

    any Other Simple method to convert this time using c# on code behind

    how to convert this using asp.net c#

     Server Time :- 07-JAN-2021 06:03:20

    Local Time :- 07-JAN-2021 18:34:00

    Tuesday, January 19, 2021 7:29 PM
  • User1535942433 posted

    Hi Gopi.MCA,

    Compiler Error Message: CS0246: The type or namespace name 'TimeZoneInfo' could not be found (are you missing a using directive or an assembly reference?)

    I'm guessing that your framework version is 2.0.And TimeZoneInfo only support more than 3.5.

    You could use ToLocalTime().But your server time need to be UTC.

    Just like this:

    Console.WriteLine("Enter a date and time in universal time.");
            strDateTime = Console.ReadLine();
    
            try {
                univDateTime = DateTime.Parse(strDateTime);
                localDateTime = univDateTime.ToLocalTime();
    
                Console.WriteLine("{0} universal time is {1} local time.",
                                         univDateTime,
                                         localDateTime);
            }
            catch (FormatException) {
                Console.WriteLine("Invalid format.");
                return;
            }

    More details,you could refer to below article:

    https://docs.microsoft.com/en-us/dotnet/api/system.datetime.tolocaltime?view=netframework-2.0

    Best regards,

    Yijing Sun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 20, 2021 9:26 AM