How to change time and timezone in iPhone simulator?

Objective CXcodeTimezoneIos Simulator

Objective C Problem Overview


How do I change time and time zone in the iPhone simulator?

Objective C Solutions


Solution 1 - Objective C

I'm guessing it uses your system timezone, so changing the TZ in System Preferences would probably do the trick

Screenshot for description

Solution 2 - Objective C

You can set the TZ environment variable in an Xcode Scheme to set the time zone just for that app.

Manage Schemes

You can use UTC, PST, EST, as well as place-based timezone names such as America/Los_Angeles. It's not well documented, but I suspect any time zone name should work.

It's not well documented, but the source is an Apple Developer Support rep on the developer forums.

Solution 3 - Objective C

Restart the Simulator after changing the system date time preferences and you shall see the changes reflected. It worked for me.

Solution 4 - Objective C

It would be useful if Apple provided a "Simulate date" as they provided a "Simulate Location" menu. What I do is set a breakpoint immediately after getting the date (saving it in a variable) and then modify the value. Or just modify the spot where I check for a date change and make it return true.

Solution 5 - Objective C

For the purpose of taking a screenshot, Apple finally made it possible to override time on the status bar of iOS simulator (since Xcode 11) by using simctl tool:

xcrun simctl status_bar "iPhone Xs" override --time "21:08"

Solution 6 - Objective C

Here is a solution available from iOS 13 and Xcode 11 at least. (did not test with previous versions) [Edit] This will change only the status bar as in the comments!

By default the iOS Simulator shows whatever the time is on your Mac, however, you can use Xcode’s command line to override that with this command in the terminal:

xcrun simctl status_bar "iPhone 11 Pro Max" override --time '9:41'

Replace the simulator name with the device you want to change.

screenshot of iPhone time

For the status bar you have this overrides:

You may specify any combination of these flags (at least one is required):

--time <string>
     Set the date or time to a fixed value.
     If the string is a valid ISO date string it will also set the date on relevant devices.
--dataNetwork <dataNetworkType>
     If specified must be one of 'wifi', '3g', '4g', 'lte', 'lte-a', or 'lte+'.
--wifiMode <mode>
     If specified must be one of 'searching', 'failed', or 'active'.
--wifiBars <int>
     If specified must be 0-3.
--cellularMode <mode>
     If specified must be one of 'notSupported', 'searching', 'failed', or 'active'.
--cellularBars <int>
     If specified must be 0-4.
--batteryState <state>
     If specified must be one of 'charging', 'charged', or 'discharging'.
--batteryLevel <int>
     If specified must be 0-100.

The time can be any string. But if you want the device to show the date you will need use the ISO format. For example a valid ISO date string would be '2007-01-09T10:41:00+01:00'

Otherwise you can use the time parameter as a string and it will display whatever you pass in it.

With thanks to the original post by Paul Hudson Here's the link!

Solution 7 - Objective C

This is an old thread but it is closest to my question. I need to simulate time zone for Europe, this method works for me. Go to 'TimeZone' tap instead of 'Date&Time' tap. Uncheck the 'Set time zone automatically using current location' box and slide the vertical rectangle bar (with blue dot on it) to simulate your system time.

enter image description here

Solution 8 - Objective C

When changing the timezone, I found the easiest way to do it was by clicking the clock in the menubar. And then selecting "Open Date & Time Preferences" then select the tab Time Zone.

Alternatively System Preferences -> Date and Time and select the tab Time Zone.

Just a pointer for anyone that might not know their way around OSX.

Solution 9 - Objective C

I have proposed an automatic solution to the problem of changing the time that includes hacky method swizzling: https://stackoverflow.com/a/34793193/829338. I assume that should also work for changing the time zone accordingly.


I needed to test my app automatically, which required changing the sys time. I did, what Tom suggested: happy hacky method swizzling.

For demonstrative purposes, I only change [NSDate date] but not [NSDate dateWithTimeIntervalSince1970:].

First your need to create your class method that serves as the new [NSDate date]. I implemented it to simply shift the time by a constant timeDifference.

int timeDifference = 60*60*24; //shift by one day
NSDate* (* original)(Class,SEL) = nil;

+(NSDate*)date{
    NSDate* date = original([NSDate class], @selector(date));
    return [date dateByAddingTimeInterval:timeDifference];
}

So far, pretty easy. Now comes the fun part. We get the methods from both classes and exchange implementation (it worked for me in the AppDelegate, but not in my UITests class). For this you will need to import objc/runtime.h.

Method originalMethod = class_getClassMethod([NSDate class], @selector(date));
Method newMethod = class_getClassMethod([self class], @selector(date));

//save the implementation of NSDate to use it later
original  = (NSDate* (*)(Class,SEL)) [NSDate methodForSelector:@selector(date)];

//happy swapping
method_exchangeImplementations(originalMethod, newMethod);

Solution 10 - Objective C

My build server is UTC and some of my unit tests needed the timezone to be PST. Using a category on NSTimeZone you can override Apple's implementation to use your code. Works for swift only projects too.

//NSTimeZone+DefaultTimeZone.h
#import <Foundation/Foundation.h>

@interface NSTimeZone (DefaultTimeZone)

+(NSTimeZone *)defaultTimeZone;

@end

//NSTimeZone+DefaultTimeZone.m
#import "NSTimeZone+DefaultTimeZone.h"

@implementation NSTimeZone (DefaultTimeZone)

+(NSTimeZone *)defaultTimeZone
{
    return [NSTimeZone timeZoneWithName:@"America/Los_Angeles"];
}

@end

Solution 11 - Objective C

After changing the system date time preferences I had to choose Hardware > Reset All Content And Settings.
Only this worked for me in Version 10.3 (SimulatorApp-880.5 CoreSimulator-681.5.4).

Solution 12 - Objective C

Only working solution for now. XCode does provide option to set a Time Zone for a particular app.

In XCode, Click on your app, Edit Scheme -> Run configuration -> Arguments Tab -> Add Environment Variables

Create a variable with Name: TZ, Value: CST (Any other standard format. XCode didn't explicitly mention the allowed values. But you can use America/Chicago too)

Image for Reference

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRahul VyasView Question on Stackoverflow
Solution 1 - Objective CnduplessisView Answer on Stackoverflow
Solution 2 - Objective CrobmathersView Answer on Stackoverflow
Solution 3 - Objective CVishal ShahView Answer on Stackoverflow
Solution 4 - Objective CLevinsonTechnologiesView Answer on Stackoverflow
Solution 5 - Objective CtadijaView Answer on Stackoverflow
Solution 6 - Objective CmultitudesView Answer on Stackoverflow
Solution 7 - Objective COhmyView Answer on Stackoverflow
Solution 8 - Objective CLasseView Answer on Stackoverflow
Solution 9 - Objective CSebastian HojasView Answer on Stackoverflow
Solution 10 - Objective CodythView Answer on Stackoverflow
Solution 11 - Objective CEvgeny KarkanView Answer on Stackoverflow
Solution 12 - Objective CAbinashView Answer on Stackoverflow