I wanna set the system timezone, date time in iOS by code. Any ideas or private apis help me? Example: Set time zone to GMT+8, and date time to Aug 10, 2013 8:30 pm. How to do it? Thanks!
US' Default Time Zone Unfortunately, there is no default time, so you have to change your time depending on the time zone used in the place.
2022 Time Zones - SwiftPST. UTC-8h.
As the others have said you can't edit the system timezone from within an app, however you can set a default timezone for your entire application using +setDefaultTimeZone:
on NSTimeZone
.
You mentioned this was for testing, so I'm going to assume this is for unit testing some custom date formatters and such, in which case you could do something like this in your unit test:
static NSTimeZone *cachedTimeZone;
@implementation DateUtilTests
+ (void)setUp
{
[super setUp];
cachedTimeZone = [NSTimeZone defaultTimeZone];
// Set to whatever timezone you want your tests to run in
[NSTimeZone setDefaultTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]];
}
+ (void)tearDown
{
[NSTimeZone setDefaultTimeZone:cachedTimeZone];
[super tearDown];
}
// ... do some tests ...
@end
I hope that helps!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With