Get the Day from the date in iOS sdk

后端 未结 7 474
无人共我
无人共我 2021-01-16 12:36

In my app, want to get the day (i.e. Sunday, Monday,etc.) from the date.

My code is as follow:

NSDate *currentDate = [NSDat         


        
相关标签:
7条回答
  • 2021-01-16 13:07
    mydate = [NSDate date];
    NSDateFormatter *dt2 = [ [NSDateFormatter alloc] init];
    [dt2 setDateFormat:@"EEEE"];
    
    NSString *day = [[NSString alloc] initWithFormat:@"%@",[dt2 stringFromDate:mydate]];
    
    0 讨论(0)
  • 2021-01-16 13:09

    i am using this and it worked for me in iOS 8.4

    -(NSString *) extractDayFromDate {
         NSCalendar* calender = [NSCalendar currentCalendar];
         NSDateComponents* component = [calender components:NSCalendarUnitWeekday fromDate:[NSDate date]];
         return [[calender weekdaySymbols] objectAtIndex:([component weekday]-1)]; 
    }
    
    0 讨论(0)
  • 2021-01-16 13:11
     NSCalendar* calender = [NSCalendar currentCalendar];
    
     NSDateComponents* component = [calender components:NSWeekdayCalendarUnit fromDate:[NSDate date]];
    
    int weekDay = [component weekday];
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    NSString *weekdayString = [[formatter weekdaySymbols] objectAtIndex:weekDay - 1];
    
    NSLog(@"Day ::: %@", weekdayString);
    
    0 讨论(0)
  • 2021-01-16 13:14

    Try this :

        NSCalendar* calender = [NSCalendar currentCalendar];
        NSDateComponents* component = [calender components:NSWeekdayCalendarUnit fromDate:[NSDate date]];
        return [component weekday]; // 1 for Sunday, 2 for Monday, and so on...
    

    and see also this Day Name From NSDate? and this https://discussions.apple.com/thread/1700102?start=0&tstart=0

    Edit

    try this

    NSString * time = @"2013-06-30T11:00:26+0100";
    
    NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease];
    [df setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"];
    NSDate *date1 = [df dateFromString:time];
    long lgTime = (long)[date1 timeIntervalSince1970];
    NSLog(@"%ld", lgTime);
    
    
    NSCalendar* calender = [NSCalendar currentCalendar];
    NSDateComponents* component = [calender components:NSWeekdayCalendarUnit fromDate:date1];
    
    NSLog(@"date=%@ === %@",[NSDate date],date1);
    NSLog(@"day= %d",[component weekday]); ///it return 1
    
    0 讨论(0)
  • 2021-01-16 13:22

    I have made a small NSDate category that helps you find the weekday name from a date. It returns the localised string for the names of the weekdays. Have a look at it. You can use it if you want to.

    NKLocalizedWeekday+NSDate

    Basically, this is the code you need:

    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [calendar components:NSWeekdayCalendarUnit fromDate:[NSDate date]];
    int weekdayNumber = [components weekday];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    NSString *weekdayString = [[formatter weekdaySymbols] objectAtIndex:weekdayNumber - 1];
    
    0 讨论(0)
  • 2021-01-16 13:33
    NSDateFormatter* theDateFormatter = [[NSDateFormatter alloc] init];
    [theDateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4];
    [theDateFormatter setDateFormat:@"EEEE"];
    NSString *weekDay =  [theDateFormatter stringFromDate:[NSDate date]];
    
    0 讨论(0)
提交回复
热议问题