iOS开发

iOS 输入日期转化为星期

2018-09-07  本文已影响1人  风规自远

/**

 * 输入日期转化为星期

 */

+ (NSString*)weekdayStringFromDate:(NSDate*)inputDate {

    NSArray *weekdays = [NSArray arrayWithObjects:[NSNull null], @"7", @"1", @"2", @"3", @"4", @"5", @"6",nil];

    NSCalendar * calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];

    NSTimeZone *timeZone = [[NSTimeZone alloc] initWithName:@"Asia/Shanghai"];

    [calendarsetTimeZone: timeZone];

    NSCalendarUnit calendarUnit = NSCalendarUnitWeekday;

    NSDateComponents* theComponents = [calendarcomponents:calendarUnitfromDate:inputDate];

    return[weekdaysobjectAtIndex:theComponents.weekday];

}

/**

 * 日期转星期

 */

+ (NSString*)weekConversionDay:(NSString*)date {

    if ([[NSString stringWithFormat:@"%@",[ZSDate weekdayStringFromDate:[ZSDate convertDateFormatStringToNSDateObjects:date]]] isEqualToString:@"1"]) {

        return@"周一";

    }else if([[NSString stringWithFormat:@"%@",[ZSDate weekdayStringFromDate:[ZSDate convertDateFormatStringToNSDateObjects:date]]] isEqualToString:@"2"]) {

        return@"周二";

    }else if([[NSString stringWithFormat:@"%@",[ZSDate weekdayStringFromDate:[ZSDate convertDateFormatStringToNSDateObjects:date]]] isEqualToString:@"3"]) {

        return@"周三";

    }else if([[NSString stringWithFormat:@"%@",[ZSDate weekdayStringFromDate:[ZSDate convertDateFormatStringToNSDateObjects:date]]] isEqualToString:@"4"]) {

        return@"周四";

    }else if([[NSString stringWithFormat:@"%@",[ZSDate weekdayStringFromDate:[ZSDate convertDateFormatStringToNSDateObjects:date]]] isEqualToString:@"5"]) {

        return@"周五";

    }else if([[NSString stringWithFormat:@"%@",[ZSDate weekdayStringFromDate:[ZSDate convertDateFormatStringToNSDateObjects:date]]] isEqualToString:@"6"]) {

        return@"周六";

    }else if([[NSString stringWithFormat:@"%@",[ZSDate weekdayStringFromDate:[ZSDate convertDateFormatStringToNSDateObjects:date]]] isEqualToString:@"7"]) {

        return@"周日";

    }

    return nil;

}

上一篇下一篇

猜你喜欢

热点阅读