How to get timezone offset as ±hh:mm?
Some integer arithmetic to obtain the offset in hours and
minutes:
let seconds = TimeZone.current.secondsFromGMT()
let hours = seconds/3600
let minutes = abs(seconds/60) % 60
Formatted printing:
let tz = String(format: "%+.2d:%.2d", hours, minutes)
print(tz) // "+01:00"
%.2d
prints an integer with (at least) two decimal digits (and leading
zero if necessary). %+.2d
is the same but with a leading +
sign for
non-negative numbers.
How to get TimeZone offset value in java
Try it like this.
String dateTime = "2021-05-27T18:47:07+0530";
String inputPattern = "yyyy-MM-dd'T'HH:mm:ssZ";
String outputPattern = "yyyy-MM-dd HH:mm:ss";
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssZ",
Locale.ENGLISH);
ZonedDateTime zdt = ZonedDateTime.parse(dateTime, dtf);
ZoneOffset tz = zdt.getOffset();
System.out.println(tz);
System.out.println(zdt.format(DateTimeFormatter.ofPattern(outputPattern,
Locale.ENGLISH)));
Prints
+05:30
2021-05-27 18:47:07
The zoned date time could also be reprinted using the same format by which it was originally parsed.
How do you get the local timezone offset in +hh:mm format?
strftime
with %z
(:
means hour and minute offset from UTC with a colon):
Time.current.strftime("%:z")
How to get datetime based on timezone offset in c#?
If you mean that you want to get the server's system local time including offset, then use the DateTimeOffset.Now
property. Then format it as desired.
DateTimeOffset.Now.ToString("yyyy-MM-dd HH:mm:ss zzz")
The zzz
specifier produces the offset as a string in the ISO 8601 extended format, that you asked for.
If what you mean is you have a UTC offset from elsewhere and you want to apply it to the current UTC time from the server, then do the following instead:
TimeSpan offset = TimeSpan.Parse("-04:00");
DateTimeOffset now = DateTimeOffset.UtcNow.ToOffset(offset);
string result = now.ToString("yyyy-MM-dd HH:mm:ss zzz");
This takes the current server time, and applies the ToOffset
function to adjust to the offset you provided.
Do keep in mind though that an offset is not the same as a time zone. The offset you have might be the one for the current date and time, or it might be for some other date and time in that time zone. For example, US Eastern Time is UTC-4 during daylight saving time, but UTC-5 during standard time. See "Time Zone != Offset" in the timezone tag wiki.
How to ISO 8601 format a Date with Timezone Offset in JavaScript?
Here's a simple helper function that will format JS dates for you.
function toIsoString(date) {
var tzo = -date.getTimezoneOffset(),
dif = tzo >= 0 ? '+' : '-',
pad = function(num) {
return (num < 10 ? '0' : '') + num;
};
return date.getFullYear() +
'-' + pad(date.getMonth() + 1) +
'-' + pad(date.getDate()) +
'T' + pad(date.getHours()) +
':' + pad(date.getMinutes()) +
':' + pad(date.getSeconds()) +
dif + pad(Math.floor(Math.abs(tzo) / 60)) +
':' + pad(Math.abs(tzo) % 60);
}
var dt = new Date();
console.log(toIsoString(dt));
Get TimeZone offset value from TimeZone without TimeZone name
I need to save the phone's timezone in the format [+/-]hh:mm
No, you don't. Offset on its own is not enough, you need to store the whole time zone name/id. For example I live in Oslo where my current offset is +02:00 but in winter (due to dst) it is +01:00. The exact switch between standard and summer time depends on factors you don't want to explore.
So instead of storing + 02:00
(or should it be + 01:00
?) I store "Europe/Oslo"
in my database. Now I can restore full configuration using:
TimeZone tz = TimeZone.getTimeZone("Europe/Oslo")
Want to know what is my time zone offset today?
tz.getOffset(new Date().getTime()) / 1000 / 60 //yields +120 minutes
However the same in December:
Calendar christmas = new GregorianCalendar(2012, DECEMBER, 25);
tz.getOffset(christmas.getTimeInMillis()) / 1000 / 60 //yields +60 minutes
Enough to say: store time zone name or id and every time you want to display a date, check what is the current offset (today) rather than storing fixed value. You can use TimeZone.getAvailableIDs()
to enumerate all supported timezone IDs.
Getting the client's time zone (and offset) in JavaScript
Using getTimezoneOffset()
You can get the time zone offset in minutes like this:
var offset = new Date().getTimezoneOffset();
console.log(offset);
// if offset equals -60 then the time zone offset is UTC+01
Related Topics
Swift:Difference in '!' and '' in Swift
Swift Programmatically Create Function for Button with a Closure
JSONencoder Won't Allow Type Encoded to Primitive Value
Error: Extraneous Argument Label 'No1:' in Call
How to Incorporate Swift Package Manager to an Existing Xcode Project
Saving Highscores with Nsuserdefaults
Shows the Alert When Uitextfield's Are Full or Empty Swift
Swift: Label Text --> "Fatal Error: Unexpectedly Found Nil While Unwrapping an Optional Value"
How to Use Uiimage(Contentsoffile:String) Method to Load Images from Images.Xcassets Folder
Using Codable to Encode/Decode from Strings to Ints with a Function in Between
Saving Swifty JSON Array to User Defaults
Why Use Float(Arc4Random())/0Xffffffff Instead of Drand()
Making a Http Post Request in Swift 2
Integrating Congnito User Pools with Amazon Cognito Identity
Find Out When Uikeyboard.Frame Intersects with Other Frame
How to Avoid Duplicate Key Error in Swift When Iterating Over a Dictionary