小白上路 iOS时间戳,字符型,date型的时间 转换为英文格式

//
//  TimeTransform.m
//  TimeFormatDemo
//
//  Created by 码修者 on 15/8/16.
//  Copyright (c) 2015年 码修者. All rights reserved.
//

#import "TimeTransform.h"

#define SET_TIME_NORMAL_FORMATTER \
NSDateFormatter* formatter = [[NSDateFormatter alloc] init];\
[formatter setDateStyle:NSDateFormatterMediumStyle];\
[formatter setTimeStyle:NSDateFormatterShortStyle];\
[formatter setDateFormat:@"yyyy-MM-dd"];

@implementation TimeTransform
#pragma mark - Public
/** 将一个时间为2015-05-20格式的字符串的月份转成英文月份
 *
 *  @param time  2015-05-20格式的字符串
 *
 *  @param abbreviations  是否使用月份缩写 是否使用日期缩写
 *
 *  @return 英文格式日期(可选) */
+ (NSString *)EngLishMonthWithTimeString:(NSString *)timeNormal abbreviations:(BOOL)abbreviations EnglishShortNameForDate:(BOOL)EnglishShortNameForDate {
    if (timeNormal.length != 10) {
        NSLog(@"时间格式错误");
        return nil;
    }
    return [self subStingOfYMD:timeNormal abbreviations:abbreviations EnglishShortNameForDate:EnglishShortNameForDate];
}


/** 将一个时间戳转换为2015-05-20格式的字符串,去掉0,再将其的月份转成英文月份
 *
 *  @param time  2015-05-20格式的字符串
 *
 *  @param abbreviations  是否使用月份缩写 是否使用日期缩写
 *
 *  @return 英文格式日期(可选) */
+ (NSString *)EngLishMonthWithTimeStamp:(NSString *)timeStamp abbreviations:(BOOL)abbreviations EnglishShortNameForDate:(BOOL)EnglishShortNameForDate
{
    SET_TIME_NORMAL_FORMATTER;//设置标准格式yyyy-mm-dd
    NSDate *changeDate = [NSDate dateWithTimeIntervalSince1970:[timeStamp doubleValue]];
    NSString *time = [formatter stringFromDate:changeDate];
    
    if (time.length != 10) {
        NSLog(@"时间格式错误");
        return nil;
    }
    
    return [self subStingOfYMD:time abbreviations:abbreviations EnglishShortNameForDate:EnglishShortNameForDate];
    
}

/** 将一个NSDate转换为2015-05-20格式的字符串,去掉0,再将其的月份转成英文月份
 *
 *  @param time  2015-05-20格式的字符串
 *
 *  @param abbreviations  是否使用月份缩写 是否使用日期缩写
 *
 *  @return 英文格式日期(可选) */
+ (NSString *)EngLishMonthWithDate:(NSDate *)date abbreviations:(BOOL)abbreviations EnglishShortNameForDate:(BOOL)EnglishShortNameForDate{
    
    SET_TIME_NORMAL_FORMATTER;//设置标准格式yyyy-mm-dd
    NSString *timeNormal = [formatter stringFromDate:date];

    if (timeNormal.length != 10) {
        NSLog(@"时间格式错误");
        return nil;
    }
    return [self subStingOfYMD:timeNormal abbreviations:abbreviations EnglishShortNameForDate:EnglishShortNameForDate];
}

#pragma mark - Private
+ (NSString *)subStingOfYMD:(NSString *)time abbreviations:(BOOL)abbreviations EnglishShortNameForDate:(BOOL)EnglishShortNameForDate{
    
    //分别截取年月日
    //month
    NSRange range;
    range.length = 2;
    range.location = 5;
    NSString * a = [time substringWithRange:range];
    int aa = [a intValue];
    NSArray * array = nil;
    
    
    if (abbreviations) {//是否使用月份缩写
        array = @[@"Jan", @"Feb", @"Mar", @"Apr", @"May", @"Jun", @"Jul", @"Aug", @"Sep", @"Oct", @"Nov", @"Dec"];
    } else {
        array = @[@"January", @"February", @"March", @"April", @"May", @"June", @"July", @"August", @"September", @"October", @"November", @"December"];
    }
    NSString * timeStr = time;
    //day
    NSString * month = array[aa - 1];
    NSRange rangeDay;
    rangeDay.length = 2;
    rangeDay.location = 8;
    NSString * day = [time substringWithRange:rangeDay];
    
    //year
    NSRange rangeYear;
    rangeYear.length = 4;
    rangeYear.location = 0;
    NSString * year = [time substringWithRange:rangeYear];
    if (EnglishShortNameForDate) {//是否使用日期缩写
        if ([day intValue] > 9) {
            timeStr = [NSString stringWithFormat:@"%@ %@th", month, day];
        } else if ([day intValue] == 1) {
            day = [day stringByReplacingOccurrencesOfString:@"0" withString:@""];
            timeStr = [NSString stringWithFormat:@"%@ %@st", month, day];
        } else if ([day intValue] == 2) {
            day = [day stringByReplacingOccurrencesOfString:@"0" withString:@""];
            timeStr = [NSString stringWithFormat:@"%@ %@nd", month, day];
        } else if ([day intValue] == 3) {
            day = [day stringByReplacingOccurrencesOfString:@"0" withString:@""];
            timeStr = [NSString stringWithFormat:@"%@ %@rd", month, day];
        } else {
            day = [day stringByReplacingOccurrencesOfString:@"0" withString:@""];
            timeStr = [NSString stringWithFormat:@"%@ %@th", month, day];
        }
        time = [NSString stringWithFormat:@"%@,%@",timeStr,year];
    }else {
        time = [NSString stringWithFormat:@"%@ %@,%@",month,day,year];
    }
    
    return time;
    
}

@end


转载于:https://my.oschina.net/u/2432042/blog/502430

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值