Tutorial by Examples

Getting current date is very easy. You get NSDate object of current date in just single line as follows: Swift var date = NSDate() Swift 3 var date = Date() Objective-C NSDate *date = [NSDate date];
The number of seconds from the current date and time for the new date. Use a negative value to specify a date before the current date. For doing this we have a method named dateWithTimerIntervalSinceNow(seconds: NSTimeInterval) -> NSDate (Swift) or + (NSDate*)dateWithTimeIntervalSinceNow:(NSTime...
There are 4 methods for comparing dates: Swift isEqualToDate(anotherDate: NSDate) -> Bool earlierDate(anotherDate: NSDate) -> NSDate laterDate(anotherDate: NSDate) -> NSDate compare(anotherDate: NSDate) -> NSComparisonResult Objective-C - (BOOL)isEqualToDate:(NSDate *)anothe...
To get Unix Epoch Time, use the constant timeIntervalSince1970: Swift let date = NSDate() // current date let unixtime = date.timeIntervalSince1970 Objective-C NSDate *date = [NSDate date]; // current date int unixtime = [date timeIntervalSince1970];
Converting an NSDate object to string is just 3 steps. 1. Create an NSDateFormatter object Swift let dateFormatter = NSDateFormatter() Swift 3 let dateFormatter = DateFormatter() Objective-C NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 2. Set the date format in whic...
There are many cases when one has created an NSDate from only an hour and minute format, i.e: 08:12 that returns from a server as a String and you initiate an NSDate instance by these values only. The downside for this situation is that your NSDate is almost completely "naked" and what yo...
Here this will calculate the UTC time offset from current data in desired timezone. +(NSTimeInterval)getUTCOffSetIntervalWithCurrentTimeZone:(NSTimeZone *)current forDate:(NSDate *)date { NSTimeZone *utcTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"]; NSInteger currentG...
Checking whether the current date contains the symbol for AM or PM Objective-C NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setLocale:[NSLocale currentLocale]]; [formatter setDateStyle:NSDateFormatterNoStyle]; [formatter setTimeStyle:NSDateFormatterShortStyle]; NSStr...
Prior to Json.NET 4.5 dates were written using the Microsoft format: "/Date(1198908717056)/". If your server sends date in this format you can use the below code to serialize it to NSDate: Objective-C (NSDate*) getDateFromJSON:(NSString *)dateString { // Expect date in this format ...
This can be used in various chat applications, rss feeds, and social apps where you need to have latest feeds with timestamps: Objective-C - (NSString *)getHistoricTimeText:(NSDate *)since { NSString *str; NSTimeInterval interval = [[NSDate date] timeIntervalSinceDate:since]; if(in...

Page 1 of 1