I am able to display the build date for my app in the simulator, but whenever I archive the app, and upload it to TestFlight, and then install it on a device, the build date doe
To get build date with format 'yyMMddHHmm' you could try this:
+ (NSString *)GetBuildDate {
NSString *buildDate;
// Get build date and time, format to 'yyMMddHHmm'
NSString *dateStr = [NSString stringWithFormat:@"%@ %@", [NSString stringWithUTF8String:__DATE__], [NSString stringWithUTF8String:__TIME__]];
// Convert to date
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"LLL d yyyy HH:mm:ss"];
NSDate *date = [dateFormat dateFromString:dateStr];
// Set output format and convert to string
[dateFormat setDateFormat:@"yyMMddHHmm"];
buildDate = [dateFormat stringFromDate:date];
[dateFormat release];
return buildDate;
}
static var buildDate: Date? {
guard let infoPath = Bundle.main.path(forResource: "Info.plist", ofType: nil) else {
return nil
}
guard let infoAttr = try? FileManager.default.attributesOfItem(atPath: infoPath) else {
return nil
}
let key = FileAttributeKey(rawValue: "NSFileCreationDate")
guard let infoDate = infoAttr[key] as? Date else {
return nil
}
return infoDate
}
static var prettyBuildDate: String {
guard let date = buildDate else {
return "Unknown"
}
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss ZZZ"
formatter.timeZone = TimeZone(abbreviation: "UTC")
return formatter.string(from: date)
}
If you redefine __DATE__
and __TIME__
it will make the time update every time you build your app. you won´t need to clean or archive to update the time, just need run the project.
#define DATE [NSString stringWithUTF8String:__DATE__]
#define TIME [NSString stringWithUTF8String:__TIME__]
- (NSString *)getBuildDate {
NSString *buildDate;
// Get build date and time, format to 'yyMMddHHmm'
NSString *dateStr = [NSString stringWithFormat:@"%@ %@", DATE , TIME ];
// Convert to date
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"LLL d yyyy HH:mm:ss"];
NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
[dateFormat setLocale:usLocale];
NSDate *date = [dateFormat dateFromString:dateStr];
// Set output format and convert to string
[dateFormat setDateFormat:@"dd/MM/yyyy-HH:mm"];
buildDate = [dateFormat stringFromDate:date];
return buildDate;
}
Swift5
let compileDate = String(Date())
let df = DateFormatter()
df.dateFormat = "MMM-dd-yyyy"
let usLocale = NSLocale(localeIdentifier: "en_US")
df.locale = usLocale
let aDate: Date? = df.date(from: compileDate)
Try running the script as a build phase step, rather than a scheme pre-action step, so it's run all the time, regardless of the type of build you are producing.
You might consider using the built-in __DATE__
and __TIME__
macros which will return a string representation of the date and time the app was built. Perhaps they will be of more help to you:
NSString *dateStr = [NSString stringWithUTF8String:__DATE__];
NSString *timeStr = [NSString stringWithUTF8String:__TIME__];