How to get NSDate day, month and year in integer format?

前端 未结 9 1059
长情又很酷
长情又很酷 2020-12-07 15:31

I want to get the day, month and year components of NSDate in integer form i.e. if the date is 1/2/1988 then I should get 1, 2 and 1988 separately as an integer

相关标签:
9条回答
  • 2020-12-07 15:52
    let date = Date()
    let calendar = Calendar.current
    
    let year = calendar.component(.year, from: date)
    let month = calendar.component(.month, from: date)
    let day = calendar.component(.day, from: date)
    print("hours = \(year):\(month):\(day)")
    
    0 讨论(0)
  • 2020-12-07 15:54
    NSDate *currentDate = [NSDate date];
    NSCalendar* calendar = [NSCalendar currentCalendar];
    NSDateComponents* components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:currentDate]; // Get necessary date components
    
    [components month]; //gives you month
    [components day]; //gives you day
    [components year]; // gives you year
    
    NSString *temp=[NSString stringWithFormat:@"Months:%d Day:%d Year:%d",[components month],  [components day],[components year]];
    NSLog(@"%@",temp);
    
    0 讨论(0)
  • 2020-12-07 15:57

    U can try this .......

    Mycal = [NSCalendar currentCalendar];
    today = [NSDate date];
    compA =[Mycal components:(NSMonthCalendarUnit | NSDayCalendarUnit | NSYearCalendarUnit | NSWeekdayCalendarUnit) fromDate:today];     
    FirstDateofMonth=[today dateByAddingTimeInterval:([compA day]-1)*(-24*60*60)];
    
    compA =[Mycal components:(NSMonthCalendarUnit | NSDayCalendarUnit | NSYearCalendarUnit | NSWeekdayCalendarUnit) fromDate:FirstDateofMonth];
    today = FirstDateofMonth;
    compA = [Mycal components:(NSMonthCalendarUnit | NSDayCalendarUnit | NSYearCalendarUnit | NSWeekdayCalendarUnit) fromDate:today];
    [compA setMonth:([compA month])];
    [self DrawMonthInPortraitView];
    [compA retain];
    
    0 讨论(0)
  • 2020-12-07 15:59

    You can use NSDateComponents to get this,

    NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
    NSDate *currDate = [NSDate date];
    NSDateComponents *dComp = [calendar components:( NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit ) 
                                          fromDate:currDate];
    
    int day = [dComp day];
    int month = [dComp month];
    int year = [dComp year];
    
    0 讨论(0)
  • 2020-12-07 16:02

    Yes by the use of NSCalendar, though, i think this will make your work.

    NSDateComponents *components = [[NSCalendar currentCalendar] components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:[NSDate date]];
    NSInteger day = [components day];    
    NSInteger month = [components month];
    NSInteger year = [components year];
    
    0 讨论(0)
  • 2020-12-07 16:06

    To those who just copy and paste directly from the web, like me, this is a small update for iOS 8

    NSCalendar* calendar = [NSCalendar currentCalendar];
    NSDateComponents* components = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:[NSDate date]];
    [components day]; //Day
    [components month];//Month
    [components year];//Year
    

    The difference is NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay NSMonthCalendarUnit | NSDayCalendarUnit | NSYearCalendarUnit | NSWeekdayCalendarUnit have been deprecated

    0 讨论(0)
提交回复
热议问题