230

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

miho
  • 11,765
  • 7
  • 42
  • 85
Rahul Vyas
  • 28,260
  • 49
  • 182
  • 256
  • 2
    to change System time and date is no solution. That impacts all Mac applications including xcode time stamps. The correct behavior is that any change of the the Simulator location has to change time and time zone. All other things are only workaround. Hope that Apple will fix this asap – Rudi Sep 02 '15 at 11:09

11 Answers11

255

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

Screenshot for description

Jayprakash Dubey
  • 35,723
  • 18
  • 170
  • 177
nduplessis
  • 12,236
  • 2
  • 36
  • 53
  • 3
    Hmm... I don't have this option on my simulator. On my phone it's under Settings -> General -> Date & Time. On my simulator, the Date & Time section doesn't exist. – Vito Andolini Jul 23 '12 at 21:37
  • 8
    Oh... change it in your machine's system preferences, not the simulator's settings. Right. That worked! – Vito Andolini Jul 23 '12 at 21:43
  • 2
    one problem in this, Simulator time is updated after 2 mints of changing the system time. any method which changes the simulator time simultaneously.... – AsifHabib Mar 07 '13 at 06:58
  • @AsifHabib where did you read this? I am wondering if there are any other quirks regarding changing system time in the simulator. I changed the time and now my simulator is yielding different results regarding NSDate operations when I first run an app on it (after deleting the app) and when I run an app that has already been installed. – SAHM Mar 17 '13 at 23:00
  • 1
    I changed the time of Mac and simulator time did not changed, I was checking either I did right steps, simulator time changed. So I concluded that simulator time is updated after 10 to 15 secs, but that is ok for me. @JPK I do not know the problem of your system, best way is to print the complete time logs and check where is problem. – AsifHabib Mar 18 '13 at 13:59
  • I found out that I am indeed having a problem related to the simulator, but it is not because I am changing the clock. Thanks for your help. – SAHM Mar 19 '13 at 16:09
  • 4
    Change system time of mac, then restart you simulator. – John Wong Sep 07 '16 at 03:54
  • This is I assume for the Mac running Xcode, this setting does not affect my simulator as my MAC time is 9AM and the simulator is 4PM. – mj662 Oct 12 '20 at 16:09
  • I changed the time on my mac and network requests failed because my certs (SSL) seem to be dependent on time – TMin Mar 31 '22 at 18:48
115

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.

robmathers
  • 3,028
  • 1
  • 26
  • 29
  • 10
    Why is this down voted? It was the only thing that worked for me actually. – Jacob Rohde Apr 21 '20 at 08:43
  • 6
    This is a really clean solution. It's also great if you want to test multiple different time zones: add a TZ setting for each one, but remember to select only one at a time. I just added America/Chicago, Europe/Istanbul, Asia/Hong_Kong, and Pacific/Honolulu to get an assortment so I can track down a timezone-dependent bug. This will NOT work if you need to capture screenshots or work with multiple apps, but it is a very fast way to switch among multiple timezones to test a single app. – Hal Mueller May 08 '20 at 01:23
  • 2
    With Xcode 12.3 / iOS 14.3 this solution works correctly only if you erase and restart the Simulator device. If you have two schemes with different TZ values set, the time zone will "stick" to the first one you run, and will show up incorrectly for the second scheme. Restarting the simulator makes it work. Groan. – Steve Mykytyn Jan 14 '21 at 00:39
  • 1
    It is not working although you restart and erase the simulator with iOS 15.2 – 93sauu Aug 30 '22 at 11:32
  • 5
    With Xcode 14.1 and iOS16.1 I can add and remove this environment variable without restarting the sim and the changes take effect when I run my App. – Brett Nov 22 '22 at 23:24
  • Is anyone able to confirm if this is working on Xcode 14.2 (14C18) and iOS 16.2? I'm trying it out now, but the Simulator keeps booting using my local time. TZ = CST – CanuckT Mar 08 '23 at 03:28
  • 1
    I can confirm that this environment variable also works when running with Swift Package Manager (e.g. `TZ=America/Chicago swift test`). Really useful! – ganzogo Mar 31 '23 at 09:51
  • This works for Unit test target. For UI Test target it doesn't (at least for me). For UI test target use code "app.launchEnvironment = ["TZ": "UTC"]" in your test case – protspace Jun 01 '23 at 09:27
48

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

Vishal Shah
  • 1,069
  • 1
  • 10
  • 11
  • 249
    Horrible solution (on Apple's part, not your answer). This is exactly the kind of thing the simulator should be able to do as a safe alternative to actually changing your system time and potentially breaking things on your computer. – devios1 Jul 05 '13 at 18:05
  • 6
    @devios this is truer than ever with  Watch – lewis Mar 02 '15 at 11:23
  • @Lewis42, what do you mean? – Iulian Onofrei Apr 15 '15 at 13:09
  • 4
    @lulian just that there's a new device for telling the time, but you still have to mess around like this to test timezones. it was a throw away comment, i was getting frustrated! :) – lewis Apr 15 '15 at 13:29
  • 1
    Very true, @Lewis42. Messing around with the system time on your phone really screws up things like movement data on your Apple Watch. Makes testing problematic, if you care about the data on your watch. – James White Nov 27 '15 at 04:15
  • My simulator gets stuck when I change system time. Is this the expected behavior? – nr5 Jun 30 '18 at 07:11
35

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"
tadija
  • 2,981
  • 26
  • 37
  • 18
    I think this only override the status bar (for the purpose of a screenshot). But it won't affect the result of a time/date API. – Roun Nov 06 '19 at 17:25
  • 3
    `xcrun simctl status_bar booted override --time "HH:MM"` seems easier – Velda Apr 23 '22 at 13:41
30

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!

multitudes
  • 2,898
  • 2
  • 22
  • 29
  • 2
    Very nice possibility, but doesn't allow really changing the timezone for testing the app functionality in a different timezone. When using e.g. xcrun simctl status_bar "iPad Pro (11-inch)" override --time '2020-01-12T10:41:00-06:00' it converts that time to my current CET timezone and sets iOS simulator time to 5:41pm – randomcontrol Jan 12 '20 at 18:32
  • 3
    For explanation: I'd like to test how my app reacts if a use travels to a different timezone. – randomcontrol Jan 12 '20 at 18:33
  • You are right, this affects only the status bar, I just did a test printing out the current time on the screen and it did not change the system time of the simulated device only the display on the top. I edited my post to reflect that. Thanks – multitudes Jan 14 '20 at 09:33
8

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

Ohmy
  • 2,201
  • 21
  • 24
3

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.

Lasse
  • 1,153
  • 1
  • 10
  • 21
  • 9
    This is not an answer, so it should be a comment. The number of iOS developers that do not know their way around OS X is...zero. – phatmann Jun 24 '16 at 18:06
2

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);
Community
  • 1
  • 1
Sebastian Hojas
  • 4,158
  • 2
  • 27
  • 38
2

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

Abinash
  • 466
  • 7
  • 15
1

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
odyth
  • 4,324
  • 3
  • 37
  • 45
  • Can you give one example where it is used ? – PJR Jul 14 '22 at 11:57
  • @PJR you only need to add the object-c category (the .h and .m) to your project and try it. In my case I tried it for the UITests without luck – 93sauu Aug 30 '22 at 11:49
0

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).

Evgeny Karkan
  • 8,782
  • 2
  • 32
  • 38