nsdate

NSDateFormatter won't format strange date string

橙三吉。 提交于 2019-12-02 00:54:06
So I have a date string I receive that looks like this: "2013-03-20T21:13:26-7:00" that I receive from a web back end. I have no control over the back end, just a fyi. My preference would be to have the date formatted like this: 9:13pm at 3/20. When I do the following NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; NSDate *date = [dateFormatter dateFromString:@"2013-03-20T21:13:26-7:00"]; date is null. My first thought is that the date string looks odd, and maybe I should remove the T and the "-7:00", as the "-7:00" is appended to every date I receive, and I'm not sure what it

Days between 2 NSDates in a calendar year swift

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-01 23:37:37
问题 I'm building a birthday reminder app. I want the user to be able to see how many days it is until somebody's next birthday. Let's say I have an NSDate() = 2015-06-30 07:21:47 +0000 And the NSDate for the birthday = 1985-08-29 12:00:00 +0000 How would I get the number of days until the next birthday? I've used something like this which gives a negative number of days since the date's actual beginning (which are in the thousands with birthdates). Then with that I would add 365 to the numerical

NSLog showing the previous Date

邮差的信 提交于 2019-12-01 23:22:42
I want to retrieve all the entries from core data added between two dates.I am using NSPredicate . As I am not getting the correct result I tried logging the date.It is showing the previous dates.After googling for a while I added [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]]; to my code.Now,Its showing correct dates but the results are still wrong. This is the code I used. NSDate *fromDate = [[self dateFormatter]dateFromString:self.fromDateField.text]; NSLog(@"%@",fromDate); NSDate *toDate = [[self dateFormatter]dateFromString:self.toDateField.text]; NSLog(@"%@",toDate)

iOS NSDateFormatter needs NSLocale even it's UTC

若如初见. 提交于 2019-12-01 22:41:05
I have a doubt that I cannot understand why is the way it is and I appeal to the Gods of this site :) I have a date coming like this: "1982-01-01T00:00:00Z" As I'm displaying whatever the server sends (I know, customer requirement, not good practice...), I'm forcing the device to have that TimeZone with the following method, simplified without error checking, not optimized, and all that kind of things: + (NSString *) yearStringFromDate: (NSDate *) date { NSDateFormatter *formatter = [NSDateFormatter new]; [formatter setDateFormat:@"YYYY"]; [formatter setTimeZone:[self timezoneForSIHF]]; return

Will two [NSDate date] following each-other always return different times?

给你一囗甜甜゛ 提交于 2019-12-01 21:17:29
Will dateA and dateB always be different? // Two [NSDate date] following each other NSDate *dateA = [NSDate date]; // Line X NSDate *dateB = [NSDate date]; // Line X+1 That is, will the line below always return NO? [dateA isEqualToDate:dateB] ("Always" meaning that like a very fast processor wouldn't execute the two commands so fast that dateA and dateB would be assigned the same time with "sub-second" accuracy). I want to have a "unique" timestamp for some internal identification (not DB-related). There is no promise that dateB will be after dateA . NSDate is based on the system clock, which

NSDateFormatter doesn't convert my NSString in NSDate

落花浮王杯 提交于 2019-12-01 21:13:41
I have a string and need convert it to a date, but it doesn't convert correctly. I don't know why... my code is: NSString * fecha = @"2011-12-07 11:11:29.657"; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss.ZZZ"]; NSDate *dateFromString = [[NSDate alloc] init]; dateFromString = [dateFormatter dateFromString:fecha]; NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; unsigned units = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit

NSDate: Right way to work with time of day?

£可爱£侵袭症+ 提交于 2019-12-01 21:12:21
I am working with a schedule that specifies times of day, such as 10:30 AM. I do not know the dates, however. I'm going to store these as values in a NSDictionary and would like to deal with them in a straightforward way. I can't use NSDate , since I don't have a date. At least, not in a straightforward way. The other way that seems obvious is NSTimeInterval , but that looks like it's probably a source of very subtle errors. I'm thinking in particular of daylight savings time (which is on my mind this week for some reason!). Other than that, the only things that really spring to mind are

Get the exact difference between 2 dates for a single NSDateComponent

岁酱吖の 提交于 2019-12-01 20:47:13
How can I get the exact difference (in decimal) between 2 values of NSDate . Eg. Jan 15 2016 to Jul 15 2017 = 1.5 Years . I can use something like: NSCalendar.currentCalendar().components(NSCalendarUnit.CalendarUnitYear, fromDate: date1, toDate: date1, options: nil).year but this gives me absolute values . i.e. for above example it would give me 1 Year . Is it possible to get exact values correct to at least a few decimal places? The terms you've used here are misleading. When you say "absolute" you mean "integral." And when you say "exact" you mean "within some desired precision." Let's say

remove time from a date like this 2016-02-10 00:00:00

ぃ、小莉子 提交于 2019-12-01 20:16:21
问题 hello I have a date like this 2016-02-10 00:00:00 I want to get only date from it in this style 14.05.2016 or 14-05-2016 This is what I have tried let dateFormatter = NSDateFormatter() let date = "2016-02-10 00:00:00" dateFormatter.dateFormat = "dd-MM-yyyy" let newdate = dateFormatter.dateFromString(date) print(newdate) //nil is coming 回答1: A better way than proposed versions is not to convert from date using a string formatter, but instead using calendar: public func removeTimeStamp(fromDate

How to get the current time instead of device time in iOS

不羁岁月 提交于 2019-12-01 20:01:56
Im using [NSDate date] to get the current time. Everything works fine if user don't change the time...I mean if user changed manually instead of setting it automatically. Then how can I get the original time? Thank you! Silviu St You can't find this out. The only way that goes through my mind is to take date from your server.. :( NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; // display in 12HR/24HR (i.e. 11:25PM or 23:25) format according to User Settings [dateFormatter setTimeStyle:NSDateFormatterShortStyle]; NSString *startTime = [dateFormatter stringFromDate:startDateTime