nsdatecomponents

format a NSDate to DDMMYYYY?

China☆狼群 提交于 2019-12-01 06:39:07
I have to send a DDMMYYY date from a Date to communicate with an API. NSDateComponents *dateComponents; NSCalendar *gregorian; NSDate *date; gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; dateComponents = [[NSDateComponents alloc] init]; self.date = [gregorian dateByAddingComponents:dateComponents toDate:[NSDate date] options:0]; Do I have to use the [dateComponents day] and so on? is there some method that can help me to performing that task like in PHP? Try this NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@

Setting NSDateComponents results in incorrect NSDate

ⅰ亾dé卋堺 提交于 2019-12-01 05:00:49
问题 I'm trying to get an NSDate object that has 21:00 as the local time - don't care about what day. I'm scratching my head at this very strange result: NSCalendar *calendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar]; NSDateComponents *components = [[NSDateComponents alloc] init]; [components setHour:21]; [components setMinute:0]; [components setSecond:0]; NSDate *date = [calendar dateFromComponents:components]; NSLog(@"%@", date); The result is 0001-01-02 04:52:58 +0000

format a NSDate to DDMMYYYY?

徘徊边缘 提交于 2019-12-01 04:27:49
问题 I have to send a DDMMYYY date from a Date to communicate with an API. NSDateComponents *dateComponents; NSCalendar *gregorian; NSDate *date; gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; dateComponents = [[NSDateComponents alloc] init]; self.date = [gregorian dateByAddingComponents:dateComponents toDate:[NSDate date] options:0]; Do I have to use the [dateComponents day] and so on? is there some method that can help me to performing that task like in PHP? 回答1

Get first day in a month from NSCalendar

五迷三道 提交于 2019-12-01 03:46:51
I have this subset of a method that needs to get day one of the current month. NSDate *today = [NSDate date]; // returns correctly 28 february 2013 NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSDateComponents *components = [[NSDateComponents alloc] init]; components.day = 1; NSDate *dayOneInCurrentMonth = [gregorian dateByAddingComponents:components toDate:today options:0]; dayOneInCurrentMonth then prints out 2013-03-01 09:53:49 +0000 , the first day of the next month. How do I get day one of the current month? Your logic is wrong: Instead of

ios - get in between dates (in actual date, not number of days)

浪尽此生 提交于 2019-11-30 10:39:29
I have two date string and I wanted to get the in between dates. For example, NSString *startDate = @"25-01-2014"; NSString *endDate = @"02-02-2014"; In between dates will be (26-01-2014, 27-01-2014, 28-01-2014.......) preferably include startDate and endDate as well. Most of the question I managed to find asked for number of days. But I needed it to be actual date. Is there anyway that I can get the in between dates? NSString *start = @"2010-09-01"; NSString *end = @"2010-12-05"; NSDateFormatter *f = [[NSDateFormatter alloc] init]; [f setDateFormat:@"yyyy-MM-dd"]; NSDate *startDate = [f

How to get NSDate for 00:00 on the day of [NSDate date]?

一曲冷凌霜 提交于 2019-11-30 06:57:09
问题 I need to get an NSDate object for 00:00(beginning of the day) from [NSDate date], let's say if currently it is 11:30am(returned by [NSDate date]), on 01/06/2012, now I need to have an NSDate for 00:00am on 01/06/2012. I haven't tried this, but what is in my mind is: NSDate *now = [NSDate date]; NSCalendar *calendar = [NSCalendar currentCalendar]; NSDateComponents *components = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit |

Swift 3.0 : Convert server UTC time to local time and vice-versa

给你一囗甜甜゛ 提交于 2019-11-30 05:50:27
问题 I want to convert server UTC time to local time and vice-versa. Here is my code.. var isTimeFromServer = true var time:String! var period:String! let timeString = "6:59 AM" //Current UTC time if isTimeFromServer { let index = timeString.index(timeString.startIndex, offsetBy: 5) let twelve = timeString.substring(to: index) var dateString:String! let dateFormatter = DateFormatter() dateFormatter.dateFormat = "H:mm" let date12 = dateFormatter.date(from: twelve)! dateFormatter.dateFormat = "h:mm

Wrong date comes back from NSDateComponents

一笑奈何 提交于 2019-11-29 18:13:44
Why is this giving me the wrong date ? NSCalendar *myCalendar = [[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar]; NSDateComponents* components = [myCalendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:[NSDate date]]; [components setYear:2013]; [components setMonth:06]; [components setDay:28]; [components setHour:5]; [components setMinute:00]; NSDate *startDate1 = [myCalendar dateFromComponents:components]; NSLog(@"start date is %@",startDate1); start date is 2013-06-28 03:00:00 +0000 EDIT What I want to do is the following. I have a start

NSDateComponents weekOfYear returns wrong date

心不动则不痛 提交于 2019-11-29 12:45:29
so i am making a basic week picker, where you can pick a week number and year, and get the corresponding date from the weeks startpoint. A basic test scenario let calendar = NSCalendar.currentCalendar() let components = NSDateComponents() components.year = 2015 components.weekOfYear = 15 print(calendar.dateFromComponents(components)!) You can see, that i set the wanted year to 2015 and the wanted week to 15, but the result i get is: "2014-12-31 23:00:00 +0000" ... and thats really gets me hanging. No matter what week and year i choose, it will always be off and in december. Thank you in

NSDate subtract one month

隐身守侯 提交于 2019-11-29 11:50:38
问题 I want two NSInteger year, month to go one month back. e.g. now is 2011-01 how can I get 2010-12 . adding is one thing, but I want to subtract. NSDate *date = [NSDate date]; NSDateComponents *dateComponents = [calendar components:unitFlags fromDate:date]; NSInteger year = [dateComponents year]; NSInteger month = [dateComponents month]; [calendar release]; NSString * pdf_name = [NSString stringWithFormat:@"file_%d_%02d.pdf", year, month]; greets endo EDIT: My solution NSCalendar *calendar= [