2

To show build date, I used these code:

    NSString *compileDate = [NSString stringWithUTF8String:__DATE__];
    NSDateFormatter *df = [[NSDateFormatter alloc]init];
    [df setDateFormat:@"MMM d yyyy"];
    NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
    [df setLocale:usLocale];
    NSDate *date = [df dateFromString:compileDate];


    [df setDateFormat:@"yyyy"];
    self.backgroundColor = [UIColor clearColor];
    self.textAlignment = NSTextAlignmentCenter;
    self.text = [NSString stringWithFormat:@"© Copyright 2009-%@ Version %@ %@", [df stringFromDate:[NSDate date]], VERSION, date ];

Today is 2014/03/15 and it returns 2014/03/14

enter image description here

My devices set up in US location. I cannot find what's wrong!

Note: The time of compile is always 20:30:00 +00:00!

How can I fix it?

Fa.Shapouri
  • 988
  • 2
  • 12
  • 30

4 Answers4

2

Just set

[df setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
iPatel
  • 46,010
  • 16
  • 115
  • 137
2

While your locale might be US, your timezone is the timezone of Iran, which is UTC+03:30.

You are creating a NSDate from a string that only contains the day component. So the time component of this date will be set to midnight. Additionally the date will be created in your local time. So it's midnight, iranian time.

And when you send description to a NSDate (as you do with the %@ format specifier) the NSString that is returned is formatted by using a UTC timezone (the +0000 at the end)

And midnight, or 00:00:00 iranian time, minus the time zone offset 03:30:00 is 20:30:00 on the previous day.

You should use another NSDateFormatter to format the build date.

E.g:

NSDateFormatter *compileDayFormatter = [[NSDateFormatter alloc] init];
compileDayFormatter.dateStyle = NSDateFormatterShortStyle;

NSString *versionInfo = [NSString stringWithFormat:@"© Copyright 2009-%@ Version %@ %@", [df stringFromDate:[NSDate date]], VERSION, [compileDayFormatter stringFromDate:date]];

or, if you don't care for a locale aware date, just use the compileDate string.

Matthias Bauch
  • 89,811
  • 20
  • 225
  • 247
1

__DATE__ contains a string which only features the date that the preprocessor was run, there is no time information associated with it. You're then logging it out directly in your label, and NSDate objects report their full time in GMT when logged. The offset you are seeing is because of the difference between your time zone and GMT.

Use the __TIME__ macro to obtain the correct time.

The macros are described here.

jrturton
  • 118,105
  • 32
  • 252
  • 268
1

My solution is to create a build.h header with the build date (build version is related to build date), and use this from my app to display the App version + build date.

Of course, build.h need to be refresh each time you build your App.

So, here is how I do this :

Select your Targets (where you can set Bundle ID ...) / Build Phases. Go to menu Editor/Add Build Phase/Add Run Script Build Phase. Move the new created line (Run Script) up to the line "Compile sources" (use drag&drop). open the line "Run script", and replace Type a script... with (please replace all  by a reverse quote ` as I don't know how to put this reverse quote in a code block ) :

echo "#define BUILD_DATE @\"date "+%d/%m/%Y %H:%M"\"" > build.h

Now each time you will build, build.h will be re-create.

So, now you need to build => you will have your 1st build.h avail at root of your project. Add it to your project.

Now, import build.h in the VC where you need the information.

Here is how I use it (I have a iboultet to a label)

- (void)viewDidLoad
{
    [super viewDidLoad];

    //cf http://stackoverflow.com/questions/3015796/how-to-programmatically-display-version-of-target-in-iphone-app-xcode
    NSString * appVersionString = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];

    self.version.text = [NSString stringWithFormat:@"v %@  %@", appVersionString,BUILD_DATE];

}
Armand DOHM
  • 1,121
  • 1
  • 7
  • 9