Convert from UTC to local timezone give wrong result
You specified only a time, but no day, therefore "09:00" is converted
to the date "2000-01-01 09:00:00 +0000". At that moment daylight saving time was not active in Stockholm, and the local time was 10:00.
If you want the conversion for the current day then you can set
dateFormatter.defaultDate = Date()
Convert to Utc give wrong result
Your timezone offset is UTC + 05.30. So when you convert your time to UTC, It will deduct 05 hours and 30 minutes from your time. So this is perfectly correct for me.
You can read more information from this MSDN article.
Strange error with converting from UTC to TimeZone
Refer to the documentation of the TimeZoneInfo.ConvertTime(DateTime, TimeZoneInfo)
method.
In the Remarks section, it says that if the Kind
property of the source DateTime
is DateTimeKind.Unspecified
, that it is assumed to be local. Since you explicitly set the kind to Unspecified
in your code, you are not converting from UTC to the named time zone, but you are instead converting from local time to the named time zone.
In this context, "local time" means local to the time zone setting of the computer where the code is executing. So if your server in Poland is set for Poland's time zone, then the conversion to Poland's time will be a no-op.
If your intention is to convert UTC to a specific time zone, then either the source value's Kind
should be DateTimeKind.Utc
, or you should use ConvertTimeFromUtc
instead of ConvertTime
(the difference being, Unspecified
kind is assumed to be Utc
rather than Local
).
As an aside - you've got a potential bug with DateTime.Parse(property.GetValue(value).ToString()
. Don't ever create a string from an object just to parse it back to an object again. Instead, cast the object to unbox it to the desired type. The tostring/parse approach often is slower and often introduces bugs related to date formats of the current culture.
Converting NSDate from UTC to local gives wrong result
NSDate objects encapsulate a single point in time, independent of any
particular calendrical system or time zone. Date objects are
immutable, representing an invariant time interval relative to an
absolute reference date (00:00:00 UTC on 1 January 2001).
Apparently you're creating the date from the .Hour
and .Minute
components, that sets the (indeterminate) year information to 01 (not 2001).
The point in time about 2000 years ago is a pretty large time interval which probably causes the weird behavior.
Convert UTC date time to local date time
Append 'UTC' to the string before converting it to a date in javascript:
var date = new Date('6/29/2011 4:52:48 PM UTC');
date.toString() // "Wed Jun 29 2011 09:52:48 GMT-0700 (PDT)"
UTC DateTime to Local and Back gives incorrect time
// This works
TimeZoneInfo tz = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time");
var convertedBack =TimeZoneInfo.ConvertTimeToUtc(localTime, tz);
Related Topics
Swift: Implementing Protocol Initializer in a Class
Updated Approach to Reauthenticate a User
"Ambiguous Use of 'Children'" When Trying to Use Nstreecontroller.Arrangedobjects in Swift 3.0
Swift Generic Protocol Function Parameters
Swift: Casting a Floatingpoint Conforming Value to Double
Swift: How to Invalidate a Timer If the Timer Starts from a Function
How to Filter Nsarray in Swift
After Getting Image from Uiimagepickercontroller, Uiimageview Rotates Image for iPhone 5
Is There Difference Between Using a Constructor and Using .Init
How to Run App in Simulator Xcode 6
Accessing Struct from One Class to Another
Swift Access Array with Index Gives Following Error. Any Idea Why
Differences Between Ways of Initializing a Dictionary
Parse.Com Pfquery Order by Time (Swift)