iOS 时间转化,将NSDate, NSString, NSNumber转化为字符串

时间转化,将NSDate, NSString, NSNumber转化为字符串

/**
 返回时间string

 @param type type=1时返回:(yyyy.MM.dd) type=2时返回:(hh:MM am/pm)type=4时返回:(yyyy-MM-dd)
 @param datestamp 传入时间戳
 @return 时间
 */
+ (NSString*)backDateFormatterOfType:(NSInteger )type Datestamp:(NSNumber*)datestamp;
+ (NSString*)backDateFormatterOfType:(NSInteger )type withDate:(NSDate *)date;
+ (NSString *)backDateFormatterOfType:(NSInteger)type timesStamp:(NSString *)timesStamp;

/**
 *  返回时间string
 *
 *  @param type      type=1时返回:(yyyy.MM.dd) type=2时返回:(hh:MM am/pm)type=4时返回:(yyyy-MM-dd)
 *  @param datestamp 传入时间戳
 *
 *  @return
 */
+ (NSString*)backDateFormatterOfType:(NSInteger )type Datestamp:(NSNumber*)datestamp
{
   
    if (!datestamp || [datestamp isEqual:[NSNull null]])
    {
        return @"";
    }
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:[datestamp longLongValue]];
    return [self backDateFormatterOfType:type withDate:date];
}

+ (NSString *)backDateFormatterOfType:(NSInteger)type timesStamp:(NSString *)timesStamp
{
    if (!timesStamp || [timesStamp isEqual:[NSNull null]])
    {
        return @"";
    }
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:[timesStamp doubleValue]];
    return [self backDateFormatterOfType:type withDate:date];
}

+ (NSString*)backDateFormatterOfType:(NSInteger )type withDate:(NSDate *)date
{
    if (!date)
    {
        return @"";
    }
    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    if (type == 1) {
        [formatter setDateFormat:@"yyyy.MM.dd"];
    }else if (type == 2)
    {
        [formatter setDateFormat:@"yyyy-MM-dd hh:mm a"];
    }else if (type == 3)
    {
        [formatter setDateFormat:@"yyyy.MM.dd a hh:mm"];
    }else if (type == 4)
    {
        [formatter setDateFormat:@"yyyy-MM-dd"];
        return [formatter stringFromDate:[NSDate date]];
    }else if (type == 5)
    {
        [formatter setDateFormat:@"yyyy-MM-dd"];
    }else if (type == 6)
    {
        [formatter setDateFormat:@"yyyy/MM/dd"];
    }else if (type == 7)
    {
        [formatter setDateFormat:@"MM/dd/yyyy"];
    }else if (type == 8)
    {
        [formatter setDateFormat:@"HH:mm"];
    }else if (type == 9)
    {
        [formatter setDateFormat:@"MM-dd HH:mm"];
    }else if (type == 10)
    {
        [formatter setDateFormat:@"yyyy-MM-dd HH:mm"];
    }else if (type == 11){
        [formatter setDateFormat:@"yyyy年MM月dd日 HH:mm"];
    }else if (type == 12){
        [formatter setDateFormat:@"yyyy年MM月dd日"];
    }else if (type == 13){
        [formatter setDateFormat:@"yyyy.MM.dd"];
    }else if (type == 14){
        [formatter setDateFormat:@"HH:mm:ss"];
    }
    return [[formatter stringFromDate:date] lowercaseString];
}

你可能感兴趣的:(iOS 时间转化,将NSDate, NSString, NSNumber转化为字符串)