nsdate

How to tell the amount of days from now until an NSDate?

流过昼夜 提交于 2019-12-10 17:52:02
问题 How can I tell the amount of days from now until the date specified in my app? Here is what I have so far, but it comes up with "-30" every time, no matter what the date is: NSDate * selected = [DatePicker date]; NSString * date = [selected description]; NSDateComponents *components = [[NSCalendar currentCalendar] components:NSDayCalendarUnit fromDate:DatePicker.date]; CalLabel1.text = [NSString stringWithFormat:@"%d", [components day]]; NSDateFormatter *temp = [[NSDateFormatter alloc] init];

Use SwiftyJSON to deserialize NSDate

梦想与她 提交于 2019-12-10 17:49:07
问题 What is the best way to deserialize a JSON date into an NSDate instance using SwiftyJSON? Is it to just use stringValue with NSDateFormatter or is there a built in date API method with SwiftyJSON? 回答1: It sounds like NSDate support isn't built in to SwiftyJSON, but you can extend the JSON class with your own convenience accessors. The following code is adapted from this GitHub issue. extension JSON { public var date: NSDate? { get { if let str = self.string { return JSON.jsonDateFormatter

NSCalendar dateFromComponents returns wrong date by 2

老子叫甜甜 提交于 2019-12-10 17:17:45
问题 I want to find out the date of the first week of a mouth in a year: NSCalendar *calendar = [NSCalendar currentCalendar]; NSDateComponents *components = [[NSDateComponents alloc] init]; [components setYear:2013]; [components setMonth:1]; [components setWeekOfMonth:1]; [components setWeekday:1]; NSDate *newDate = [calendar dateFromComponents:components]; NSLog(@"%@",newDate); What I get is: 2012-12-29 23:00:00 +0000 And when I compare to my mac calendar what I need to get is: 2012-12-31 23:00

How to convert buddhist Date To Gregorian Date Properly iOS

亡梦爱人 提交于 2019-12-10 16:13:16
问题 How to convert Jan 27, 2557 a Buddhist date into Jan 27, 2013 . Yes, I'm aware that If I subtract 543 from 2557 I'll get 2014. However, I want it to solve using NSDateFormatter. Here my code: dateFormatter = [[NSDateFormatter alloc] init]; dateFormatter.calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; TimeZone *timeZone = [NSTimeZone timeZoneWithName:@"UTC"]; [dateFormatter setTimeZone:timeZone]; [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSSSS'Z'"]; /

Apple Swift: how to get current seconds in 1/100 or 1/1000?

青春壹個敷衍的年華 提交于 2019-12-10 15:34:59
问题 func updateTime() { var date = NSDate() var calendar = NSCalendar.currentCalendar() var components = calendar.components(.CalendarUnitSecond, fromDate: date) var hour = components.hour var minutes = components.minute var seconds = components.second counterLabel.text = "\(seconds)" var myIndicator = counterLabel.text?.toInt() if myIndicator! % 2 == 0 { // do this } else { // do that } } I'd like to know how I can change this code so I get 1/10 or 1/100 or 1/1000 of a second to display in

IOS: problem with NSDate

久未见 提交于 2019-12-10 15:28:40
问题 I have this code: NSDateComponents *components = [[[NSDateComponents alloc] init] autorelease]; [components setYear:2011]; [components setDay:1]; [components setMonth:4]; NSCalendar *gregorianCalendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease]; NSDate *firstDate = [gregorianCalendar dateFromComponents:components]; NSLog(@"date:%@", firstDate); the result in console is: date:2011-03-31 why???? if I set "setDay:1" it would be "date:2011-04-01", no? 回答1:

NSDate behaviour when saving to plist

柔情痞子 提交于 2019-12-10 14:26:15
问题 I am creating a date from [NSdate date] and saving it to plist, Here is how I am creating a date - (void)applicationWillResignActive:(UIApplication *)application { NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; // [gregorian setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]]; NSDateComponents *weekdayComponents = [gregorian components:(NSDayCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit | NSMinuteCalendarUnit)fromDate:[NSDate date]

Different result for [NSDate date] in several devices

折月煮酒 提交于 2019-12-10 14:18:51
问题 To start, I have to say that I set autoset in date&time settings and time zone is the same for each device. So I use [NSDate date] to get time stamp in milliseconds, then encode to NSData and send to another device. On receiver data is being decoded and subtract with new [NSDate date] . So that I get total time needed for send and receive message. That I was thought because when sender is iPhone 4 iOS6 and receiver is iPhone 5 iOS7 then receiver have earlier time stamp than sender. I don't

How to convert the Date String with time zone to NSDate

試著忘記壹切 提交于 2019-12-10 14:00:00
问题 How to convert the Date String "2012-05-03 06:03:00 +0000" to NSDate. I user the code below, but it does not work: NSDateFormatter *datFormatter = [[NSDateFormatter alloc] init]; [datFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss Z"]; NSDate* date = [datFormatter dateFromString:dateStr]; 回答1: NSString *dateStr = @"2012-05-03 06:03:00 +0000"; NSDateFormatter *datFormatter = [[NSDateFormatter alloc] init]; [datFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss Z"]; NSDate* date = [datFormatter

Create NSDate with a specific time

雨燕双飞 提交于 2019-12-10 13:27:21
问题 I have the following problem. I need to create an NSDate object from a specific time. The time is 6 pm for today in a given timezone (sometimes CST, EST etc). Not 6 pm for every day, but 6pm for today in that particular timezone. I was thinking of using the NSCalendar object to create the specific date/time but the problem is that it needs a day, month and year. So how do I set these values when I don't have the correct date for it. Example : Today is 28th January 2014, 11:09 in CST. Now if I