Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 77050

How to get Local time for a specific zone using .NET Standard?

$
0
0

Hi,

I am using this in C# but I want to get the same in Xamarin in order to get the current time for Dubai.

How to achieve that?

I tried the same code but getting:

System.TimeZoneNotFoundException: Arabian Standard Time
at System.TimeZoneInfo.GetMonoTouchData (System.String name, System.Boolean throw_on_error) [0x00012] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.12.0.4/src/Xamarin.iOS/mcs/class/corlib/System/TimeZoneInfo.MonoTouch.cs:98
at System.TimeZoneInfo.FindSystemTimeZoneByIdCore (System.String id) [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.12.0.4/src/Xamarin.iOS/mcs/class/corlib/System/TimeZoneInfo.MonoTouch.cs:56
at System.TimeZoneInfo.FindSystemTimeZoneById (System.String id) [0x00021] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.12.0.4/src/Xamarin.iOS/mcs/class/corlib/System/TimeZoneInfo.cs:574
at Jafaria.HomePage.GetDubaiLocalTime () [0x00001] in /Users/jassim/Projects/Jafaria/Jafaria/HomePage.xaml.cs:274
at Jafaria.HomePage+d__15.MoveNext () [0x0001e] in /Users/jassim/Projects/Jafaria/Jafaria/HomePage.xaml.cs:306
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.12.0.4/src/Xamarin.iOS/mcs/class/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs:152
at System.Runtime.CompilerServices.AsyncMethodBuilderCore+<>c.b__6_0 (System.Object state) [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.12.0.4/src/Xamarin.iOS/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/AsyncMethodBuilder.cs:1018
at UIKit.UIKitSynchronizationContext+<>c__DisplayClass1_0.b__0 () [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.12.0.4/src/Xamarin.iOS/UIKit/UIKitSynchronizationContext.cs:24
at Foundation.NSAsyncActionDispatcher.Apply () [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.12.0.4/src/Xamarin.iOS/Foundation/NSAction.cs:125
at at (wrapper managed-to-native) UIKit.UIApplication.UIApplicationMain(int,string[],intptr,intptr)
at UIKit.UIApplication.Main (System.String[] args, System.IntPtr principal, System.IntPtr delegate) [0x00005] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.12.0.4/src/Xamarin.iOS/UIKit/UIApplication.cs:79
at UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x0002c] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.12.0.4/src/Xamarin.iOS/UIKit/UIApplication.cs:63
at Jafaria.iOS.Application.Main (System.String[] args) [0x00001] in /Users/jassim/Projects/Jafaria/iOS/Main.cs:18

here is the code:

TimeZoneInfo DubaiTimeZone = TimeZoneInfo.FindSystemTimeZoneById("Arabian Standard Time");
DateTime utc = DateTime.UtcNow;
DateTime DubaiLocalTime = TimeZoneInfo.ConvertTimeFromUtc(utc, DubaiTimeZone);

// DateTimeOffset DubaiLocalTime = TimeZoneInfo.ConvertTime(DateTimeOffset.UtcNow, TimeZoneInfo.FindSystemTimeZoneById("Arabian Standard Time"));

LabelHomeMageLocalTime.Text = DubaiLocalTime.ToString("HH:mm");

Thanks,
Jassim


Viewing all articles
Browse latest Browse all 77050

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>