Get current NSDate in timestamp format

Here’s what I use: NSString * timestamp = [NSString stringWithFormat:@”%f”,[[NSDate date] timeIntervalSince1970] * 1000]; (times 1000 for milliseconds, otherwise, take that out) If You’re using it all the time, it might be nice to declare a macro #define TimeStamp [NSString stringWithFormat:@”%f”,[[NSDate date] timeIntervalSince1970] * 1000] Then Call it like this: NSString * timestamp = TimeStamp; … Read more

Difference between ‘YYYY’ and ‘yyyy’ in NSDateFormatter

Also when using a date format string using the correct format is important. @”YYYY” is week-based calendar year. @”yyyy” is ordinary calendar year. You can go through the whole blog, its a good to give it a look https://web.archive.org/web/20150423093107/http://realmacsoftware.com/blog/working-with-date-and-time http://realmacsoftware.com/blog/working-with-date-and-time (dead link)

Convert NSDate to String in iOS Swift [duplicate]

you get the detail information from Apple Dateformatter Document.If you want to set the dateformat for your dateString, see this link , the detail dateformat you can get here for e.g , do like let formatter = DateFormatter() // initially set the format based on your datepicker date / server String formatter.dateFormat = “yyyy-MM-dd HH:mm:ss” … Read more

Xcode Swift am/pm time to 24 hour format

Just convert it to a date using NSDateFormatter and the “h:mm a” format and convert it back to a string using the “HH:mm” format. Check out this date formatting guide to familiarize yourself with this material. let dateAsString = “6:35 PM” let dateFormatter = NSDateFormatter() dateFormatter.dateFormat = “h:mm a” dateFormatter.locale = Locale(identifier: “en_US_POSIX”) // fixes … Read more

How to get NSDate day, month and year in integer format?

Here you are, NSDate *currentDate = [NSDate date]; NSCalendar* calendar = [NSCalendar currentCalendar]; NSDateComponents* components = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:currentDate]; // Get necessary date components [components month]; //gives you month [components day]; //gives you day [components year]; // gives you year You can use NSDateComponents for that as above. Please visit this page for details.

How do I get an ISO 8601 date on iOS?

Use NSDateFormatter: NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; NSLocale *enUSPOSIXLocale = [NSLocale localeWithLocaleIdentifier:@”en_US_POSIX”]; [dateFormatter setLocale:enUSPOSIXLocale]; [dateFormatter setDateFormat:@”yyyy-MM-dd’T’HH:mm:ssZZZZZ”]; [dateFormatter setCalendar:[NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian]]; NSDate *now = [NSDate date]; NSString *iso8601String = [dateFormatter stringFromDate:now]; And in Swift: let dateFormatter = DateFormatter() let enUSPosixLocale = Locale(identifier: “en_US_POSIX”) dateFormatter.locale = enUSPosixLocale dateFormatter.dateFormat = “yyyy-MM-dd’T’HH:mm:ssZZZZZ” dateFormatter.calendar = Calendar(identifier: .gregorian) let iso8601String = … Read more

Formatting NSDate into particular styles for both year, month, day, and hour, minute, seconds

this is what i used: NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; [dateFormat setDateFormat:@”yyyy-MM-dd”]; NSDateFormatter *timeFormat = [[NSDateFormatter alloc] init]; [timeFormat setDateFormat:@”HH:mm:ss”]; NSDate *now = [[NSDate alloc] init]; NSString *theDate = [dateFormat stringFromDate:now]; NSString *theTime = [timeFormat stringFromDate:now]; NSLog(@”\n” “theDate: |%@| \n” “theTime: |%@| \n” , theDate, theTime); [dateFormat release]; [timeFormat release]; [now release];