nsdate

Objective-C setting NSDate to current UTC

爷,独闯天下 提交于 2019-11-27 02:59:26
Is there an easy way to init an NSDate with the current UTC date/time? jessecurry [NSDate date]; You may want to create a category that does something like this: -(NSString *)getUTCFormateDate:(NSDate *)localDate { NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"UTC"]; [dateFormatter setTimeZone:timeZone]; [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; NSString *dateString = [dateFormatter stringFromDate:localDate]; [dateFormatter release]; return dateString; } NSDate is a reference to an interval from an absolute

Increase NSDate +1 day method Objective-C?

笑着哭i 提交于 2019-11-27 02:44:00
问题 This is my method I use for increasing by one day in my navigationBar, and setting the name of the day as a title. I know it's wrong because I set "today" variable every time its called. But I can't figure out how to increase +1 day every time I call this method. -(void)stepToNextDay:(id)sender { today = [NSDate date]; NSDate *datePlusOneDay = [today dateByAddibgTimeInterval:(60 * 60 * 24)]; NSDateFormatter *dateformatterBehaviour = [[[NSDateFormatter alloc]init]autorelease]; [dateFormatter

Swift - Convert Milliseconds into Minutes, Seconds and Milliseconds

萝らか妹 提交于 2019-11-27 02:27:49
问题 I am writing a racing app and want to convert a large number of milliseconds into minutes:seconds.milliseconds (for example, 1:32.361 ). At the moment I just do it through maths ( minutes / 60000 ), then get remainder and divide further, etc., but i find that it is off every now and then by 1 millisecond. I know this might not sound like a big deal, but I tried to find the average of 1:32.004 and 1:32.004 and it returned 1:32.003 , which means it gets put in incorrectly and the rest of the

Nil NSDate when trying to get date from UTC string in zulu time

扶醉桌前 提交于 2019-11-27 02:12:20
问题 Writing an iPhone app in Objective-C, I have a date in string form (in UTC format, with a Z on the end to denote zero UTC offset, or zulu time), which I need to parse into an NSDate object. A bit of code: NSDateFormatter* df = [[NSDateFormatter alloc]init]; [df setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSSZ"]; NSString* str = @"2009-08-11T06:00:00.000Z"; NSDate* date = [df dateFromString:str]; Running this through the debugger, date ends up nil ! I'm assuming it has something to do with my date

NSDateFormatter dateFromString returns incorrect date [duplicate]

允我心安 提交于 2019-11-27 01:57:22
问题 This question already has an answer here: Get NSDate from NSDate adjusted with timezone 2 answers I am trying to use NSDateFormatter in my app which takes a date string and formats it to an NSDate so that I can do Date Comparisons, however I am finding when I use dateFromString and format it the date is losing one day. NSString *dateString = @"02-06-2012"; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"dd-MM-yyyy"]; NSDate *dateFromString = [

Convert date string swift

强颜欢笑 提交于 2019-11-27 01:55:09
问题 I have a date string in this format: 2016-06-20T13:01:46.457+02:00 and I need to change it to something like this: 20/06/2016 Previously I have always used this library -> SwiftDate to manipulate the dates, but it doesn't work now. I tried also something like: let myDate = self.dateNoteDict[indexPath.row]! let dateFormatter = NSDateFormatter() dateFormatter.dateFormat = "yyyy-MM-dd hh:mm:ss.SSSSxxx" let date = dateFormatter.dateFromString(myDate) print("date -> \(date)") but it doesn't work.

Check if date is before current date (Swift)

柔情痞子 提交于 2019-11-27 01:45:41
问题 I would like to check if a NSDate is before (in the past) by comparing it to the current date. How would I do this? Thanks 回答1: I find the earlierDate method. if date1.earlierDate(date2).isEqualToDate(date1) { print("date1 is earlier than date2") } You also have the laterDate method. Swift 3 to swift 5: if date1 < date2 { print("date1 is earlier than date2") } 回答2: There is a simple way to do that. (Swift 3 is even more simple, check at end of answer) Swift code: if myDate

how do I set an existing NSDate's time?

こ雲淡風輕ζ 提交于 2019-11-27 01:17:07
问题 how do I set an existing NSDate 's time? That is I have a date (say current date/time), but then want to set this to a specific time (e.g. 11.23am) say. What is the quickest way to do this in objective C? I'm looking at NSCalendar and see methods such as dateByAddingComponents:toDate:options: , and dateFromComponents: , but these don't seem to quite hit the mark for what I need here. For example: dateFromComponents: - I would have to work out all components for this to work which seems like

converting timestamp to nsdate format

橙三吉。 提交于 2019-11-27 01:13:56
问题 I want to convert my timestamp value 1308031456 to NSDate format (which yields the value Tue, 14 Jun 2011 06:04:16 GMT in online web conversion). How would I convert between the two programmatically? 回答1: OBJ - C: Use dateWithTimeIntervalSince1970:, NSDate *date = [NSDate dateWithTimeIntervalSince1970:timeStamp]; SWIFT: Use init(timeIntervalSince1970:) let date = NSDate(timeIntervalSince1970:timeStamp) 回答2: You can use the use the method dateWithTimeIntervalSince1970: to convert the timestamp

Get UTC time and local time from NSDate object

99封情书 提交于 2019-11-27 00:08:43
In objective-c, the following code results in the UTC date time information using the date API. NSDate *currentUTCDate = [NSDate date] In Swift however, let date = NSDate.date() results in local date and time. I have two questions: How can I get UTC time and local time (well date gives local time) in NSDate objects. How can I get precision for seconds from the NSDate object. EDIT 1: Thanks for all the inputs but I am not looking for NSDateFormatter objects or string values. I am simply looking for NSDate objects (however we cook them up but that's the requirement). See point 1. The