nsdate

How can I set an NSDate object to midnight?

怎甘沉沦 提交于 2019-11-26 12:39:37
问题 I have an NSDate object and I want to set it to an arbitrary time (say, midnight) so that I can use the timeIntervalSince1970 function to retrieve data consistently without worrying about the time when the object is created. I\'ve tried using an NSCalendar and modifying its components by using some Objective-C methods, like this: let date: NSDate = NSDate() let cal: NSCalendar = NSCalendar(calendarIdentifier: NSGregorianCalendar)! let components: NSDateComponents = cal.components

Minimum and maximum date in UIDatePicker

纵然是瞬间 提交于 2019-11-26 12:39:24
问题 I want to get the minimum and maximum date from a date picker, but minimum date should be \"- 18\" of the current date and the maximum date should be \"- 100\" of current date. Suppose current year is 2018 then I want minimum date 2000 and maximum date 1918. What I have done so far is : NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSDateComponents *components = [gregorian components:NSYearCalendarUnit fromDate:[NSDate date]]; NSInteger year =

Fuzzy date algorithm

好久不见. 提交于 2019-11-26 12:37:49
问题 I\'m looking for a fuzzy date algorithm. I just started writing one and realised what a tedious task it is. It quickly degenerated into a lot of horrid code to cope with special cases like the difference between \"yesterday\", \"last week\" and \"late last month\" all of which can (in some cases) refer to the same day but are individually correct based on today\'s date. I feel sure there must be an open source fuzzy date formatter but I can\'t find it. Ideally I\'d like something using NSDate

How to parse a date string into an NSDate object in iOS?

橙三吉。 提交于 2019-11-26 12:21:50
I am trying to parse a date string from xml into an NSDate object in an iPhone app I am aware this must have been asked before, however, I believe I have the right syntax, but it is not working. Is there a problem with my code? The date string I need to parse is: 2011-01-21T12:26:47-05:00 The code I am using to parse it is: self.dateFormatter = [[NSDateFormatter alloc] init]; [self.dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]]; [self.dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"] autorelease]]; [self.dateFormatter setDateFormat:@"yyyy'

NSDate timeIntervalSince1970 not working in Swift?

北战南征 提交于 2019-11-26 12:19:59
问题 I am doing this in swift: let date = NSDate(timeIntervalSince1970: 1432233446145.0) println(\"date is \\(date)\") The log gives me this: date is 47355-09-02 23:55:45 +0000 Then I try to get an interval back out just for testing: let tI = expirationDate.timeIntervalSinceDate(date) println(\"tI = \\(tI)\") I get: tI = 0.0 What am I doing wrong? I can seem to make the timeIntervalSince1970 call work properly. Is there any known issued with that in Swift, or am I missing something here? 回答1:

iOS: Compare two dates

老子叫甜甜 提交于 2019-11-26 12:09:59
I have a NSDate that I must compare with other two NSDate and I try with NSOrderAscending and NSOrderDescending but if my date is equal at other two dates? Example: if I have a myDate = 24/05/2011 and other two that are one = 24/05/2011 and two 24/05/2011 what can I use? According to Apple documentation of NSDate compare: Returns an NSComparisonResult value that indicates the temporal ordering of the receiver and another given date. - (NSComparisonResult)compare:(NSDate *)anotherDate Parameters anotherDate The date with which to compare the receiver. This value must not be nil. If the value is

How to compare two NSDates: Which is more recent?

蓝咒 提交于 2019-11-26 12:02:39
I am trying to achieve a dropBox sync and need to compare the dates of two files. One is on my dropBox account and one is on my iPhone. I came up with the following, but I get unexpected results. I guess I'm doing something fundamentally wrong when comparing the two dates. I simply used the > < operators, but I guess this is no good as I am comparing two NSDate strings. Here we go: NSLog(@"dB...lastModified: %@", dbObject.lastModifiedDate); NSLog(@"iP...lastModified: %@", [self getDateOfLocalFile:@"NoteBook.txt"]); if ([dbObject lastModifiedDate] < [self getDateOfLocalFile:@"NoteBook.txt"]) {

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

試著忘記壹切 提交于 2019-11-26 12:00:18
I basically need to get current date and time separately, formatted as: 2009-04-26 11:06:54 The code below, from another question on the same topic, generates now: |2009-06-01 23:18:23 +0100| dateString: |Jun 01, 2009 23:18| parsed: |2009-06-01 23:18:00 +0100| This is almost what I'm looking for, but I want to separate the day and time. NSDateFormatter *format = [[NSDateFormatter alloc] init]; [format setDateFormat:@"MMM dd, yyyy HH:mm"]; NSDate *now = [[NSDate alloc] init]; NSString *dateString = [format stringFromDate:now]; NSDateFormatter *inFormat = [[NSDateFormatter alloc] init];

Get current iPhone device timezone date and time from UTC-5 timezone date and time iPhone app?

好久不见. 提交于 2019-11-26 11:57:00
问题 In my iPhone application i am very struggling to convert the webservice response time to current device time. The webservice time in UTC-5 timezone . The app is worldwide use app. So i have to convert the response time to current device timezone. This is simple messaging app. When i have sent a message the time is \"5:05 PM\" (India Chennai timezone) but when am retrieve time from webservice is \"2012-07-16 07:33:01\" . When i show the time in the app i should convert the time to device\'s

Difference between two NSDate objects — Result also a NSDate

亡梦爱人 提交于 2019-11-26 11:56:44
问题 I have two NSDate objects and I want the difference between the two and the result should again be a NSDate object. Any idea how to achieve this? Here, I am trying to address a unique problem where I have to find out the elapsed time and then localize the elapsed time. I can localize it if I have the elapsed time in NSDate object. So thought of creating a NSDate object which has its time component same as time interval between the two dates so that I could use NSDateFormatter to localize it.