日期字符串转化为年月日

简介: 日期字符串转化为年月日

若你只是想把绝对日期字符串(至1970到现在的毫秒数)转化为年月日,时分秒,可以建立一个分类,使用系统函数自己转化。不需要我上篇文章说的完全自己实现日期转化函数。

注意:

服务器默认记路的日期字符串是精确的毫秒,苹果是默认精确到秒。

调用例子:

    NSString *time = [NSString stringWithFormat:@"%lld", model.startPoint.obtainTime*1000];
    _dateLabel.text = [time dateFomatterStringWithMD];


NSString+Extension.h

#import <Foundation/Foundation.h>

@interface NSString (Extension)
/**
 *  把时间戳格式化为 MM月dd日 EEE HH:mm 的格式
 */
- (NSString *)dateFomatterStringWithMDEHM;

/**
 *  把时间戳格式化为 yyyy年MM月dd日 HH:mm 的格式
 */
- (NSString *)dateFomatterStringWithYMDHM;
/**
 *  把时间戳格式化为 yyyy.MM.dd HH:mm 的格式
 */
- (NSString *)dateFomatterStringWithymdhm;
/**
 *  把时间戳格式化为 MM月dd日 HH:mm 的格式
 */
- (NSString *)dateFomatterStringWithMDHM;
/**
 *  把时间戳格式化为 HH:mm 的格式
 */
- (NSString *)dateFomatterStringWithHM;
/**
 *  把时间戳格式化为今天(*日) HH:mm 的格式
 */
- (NSString *)dateFomatterStringWithDayHM;
/**
 *  把时间戳格式化为 MM月dd日的格式
 */
- (NSString *)dateFomatterStringWithMD;
/**
 *  把时间戳格式化为 yyyy-MM-dd 的格式
 */
- (NSString *)dateFomatterStringWithYMD;
@end

NSString+Extension.m

#import "NSString+Extension.h"

@implementation NSString (Extension)

#pragma mark - 把时间戳格式化为 MM月dd日 EEE HH:mm 的格式
- (NSString *)dateFomatterStringWithMDEHM;
{
    FLDDLogDebug(@"函数");
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"MM月dd日 EEE HH:mm"];
    NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:[self longLongValue]/1000];
    return [formatter stringFromDate:confromTimesp];
}
#pragma mark - 把时间戳格式化为 yyyy年MM月dd日 HH:mm 的格式
- (NSString *)dateFomatterStringWithYMDHM;
{
    FLDDLogDebug(@"函数");
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"yyyy年MM月dd日 HH:mm"];
    NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:[self longLongValue]/1000];
    return [formatter stringFromDate:confromTimesp];
}
#pragma mark - 把时间戳格式化为 yyyy.MM.dd HH:mm 的格式
- (NSString *)dateFomatterStringWithymdhm
{
    FLDDLogDebug(@"函数");
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"yyyy.MM.dd HH:mm"];
    NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:[self longLongValue]/1000];
    return [formatter stringFromDate:confromTimesp];
}
#pragma mark - 把时间戳格式化为 yyyy的格式
- (NSString *)dateFomatterStringWithy
{
    FLDDLogDebug(@"函数");
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"yyyy"];
    NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:[self longLongValue]/1000];
    return [formatter stringFromDate:confromTimesp];
}
#pragma mark - 把时间戳格式化为 MM月dd日 HH:mm 的格式
- (NSString *)dateFomatterStringWithMDHM {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"MM月dd日 HH:mm"];
    NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:[self longLongValue]/1000];
    return [formatter stringFromDate:confromTimesp];
}


#pragma mark - 把时间戳格式化为 MM月dd日的格式
- (NSString *)dateFomatterStringWithMD {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"MM月dd日"];
    NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:[self longLongValue]/1000];
    return [formatter stringFromDate:confromTimesp];
}
#pragma mark - 把时间戳格式化为 HH:mm 的格式
- (NSString *)dateFomatterStringWithHM {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"HH:mm"];
    NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:[self longLongValue]/1000];
    return [formatter stringFromDate:confromTimesp];
}

#pragma mark - 把时间戳格式化为今天(*日) HH:mm 的格式
- (NSString *)dateFomatterStringWithDayHM {
    long long nowTime = (long long)([[NSDate date] timeIntervalSince1970] * 1000);
    if([self longLongValue] < 100000)
    {
        return @"未知时间";
    }
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    if(ABS(nowTime - [self longLongValue]) > 3600000)
    {
        [formatter setDateStyle:NSDateFormatterMediumStyle];
        [formatter setTimeStyle:NSDateFormatterShortStyle];
        [formatter setDateFormat:@"dd日"];
        NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:[self longLongValue]/1000];
        NSString *dayStr = [formatter stringFromDate:confromTimesp];

        formatter = [[NSDateFormatter alloc] init];
        [formatter setDateStyle:NSDateFormatterMediumStyle];
        [formatter setTimeStyle:NSDateFormatterShortStyle];
        [formatter setDateFormat:@"HH:mm"];
        confromTimesp = [NSDate dateWithTimeIntervalSince1970:[self longLongValue]/1000];
        NSString *hourMinuteStr = [formatter stringFromDate:confromTimesp];
        return [[NSString alloc] initWithFormat:@"%@ %@",dayStr,hourMinuteStr];
    }
    else
    {
        [formatter setDateStyle:NSDateFormatterMediumStyle];
        [formatter setTimeStyle:NSDateFormatterShortStyle];
        [formatter setDateFormat:@"dd日"];
        NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:[self longLongValue]/1000];
        NSString *dayStr = [formatter stringFromDate:confromTimesp];

        confromTimesp = [NSDate dateWithTimeIntervalSince1970:nowTime/1000];
        NSString *todayStr = [formatter stringFromDate:confromTimesp];
        if([dayStr isEqualToString: todayStr])
        {
            formatter = [[NSDateFormatter alloc] init];
            [formatter setDateStyle:NSDateFormatterMediumStyle];
            [formatter setTimeStyle:NSDateFormatterShortStyle];
            [formatter setDateFormat:@"HH:mm"];
            confromTimesp = [NSDate dateWithTimeIntervalSince1970:[self longLongValue]/1000];
            NSString *hourMinuteStr = [formatter stringFromDate:confromTimesp];
            return [[NSString alloc] initWithFormat:@"今天 %@",hourMinuteStr];
        }
        else
        {
            formatter = [[NSDateFormatter alloc] init];
            [formatter setDateStyle:NSDateFormatterMediumStyle];
            [formatter setTimeStyle:NSDateFormatterShortStyle];
            [formatter setDateFormat:@"HH:mm"];
            confromTimesp = [NSDate dateWithTimeIntervalSince1970:[self longLongValue]/1000];
            NSString *hourMinuteStr = [formatter stringFromDate:confromTimesp];
            return [[NSString alloc] initWithFormat:@"%@ %@",dayStr,hourMinuteStr];
        }

    }

}

/**
 *  把时间戳格式化为 yyyy-MM-dd 的格式
 */
- (NSString *)dateFomatterStringWithYMD;
{
    if([self longLongValue] < 100000)
    {
        return @"未知时间";
    }
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:[self longLongValue]/1000];
    return [formatter stringFromDate:confromTimesp];
}
@end



目录
相关文章
将时间格式转化成时间戳和时间戳转化成时间格式的公式
将时间格式转化成时间戳和时间戳转化成时间格式的公式
|
2月前
|
Java
自定义字符串转换为周几和指定日期格式
自定义字符串转换为周几和指定日期格式
29 0
|
8月前
|
JSON JavaScript 前端开发
uniapp时间戳转换成年月日时分秒的格式
uniapp时间戳转换成年月日时分秒的格式
176 1
uniapp时间戳转换成年月日时分秒的格式
|
9月前
时间戳转化成日期
时间戳转化成日期
39 0
|
11月前
计算日期到天数转换
计算日期到天数转换
61 0
|
12月前
|
前端开发 数据格式
前端实现年月日时分秒的转换
前端实现年月日时分秒的转换
121 0
dateTime怎么根据日期(年月日)查找数据
dateTime怎么根据日期(年月日)查找数据
128 0
dateTime怎么根据日期(年月日)查找数据
日期获取方法
日期获取方法
163 0
timeval 转换为年月日
timeval 转换为年月日
143 0
timeval 转换为年月日
日期字符串转换成时间戳
日期字符串转换成时间戳
150 0