关于NSDate时间的基础使用

2015-05-27 0 718
关于NSDate时间的基础使用
#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //获取本地时间
    NSDate *date = [NSDate date];
    NSLog(@"当前时间%@",date);
    //格式化 时间
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    NSTimeZone *timeZone = [NSTimeZone localTimeZone];
    [formatter setTimeZone:timeZone];
    [formatter setDateFormat : @"yyyy-MM-dd HH:mm:ss"];
    NSString *string;
    //转换成字符串
    string = [NSString stringWithFormat:@"%@",[formatter stringFromDate:date]];
    NSLog(@"%@",string);
    NSString *str;
    //距离1970多少201323323333毫秒之后的时间
    str = [NSString stringWithFormat:@"%@",[formatter stringFromDate:[NSDate dateWithTimeIntervalSince1970:201323323333]]];
    NSLog(@"%@",str);
    //获取当前时间距离1970的毫秒数
    UInt64 recordTime = [[NSDate date] timeIntervalSince1970]*1000;
    NSLog(@"%llu",recordTime);
    
    
    
}
// 转换UTC时间格式 传入date UTC格式
- (NSString *)formatDateWithUTC:(NSDate *)date
{
    
    NSString *str = [NSString stringWithFormat:@"%@",date];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    //输入格式
    [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSSZ"];
    dateFormatter.timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];
    NSDate *dateFormatted = [dateFormatter dateFromString:str];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSString *locationTimeString=[dateFormatter stringFromDate:dateFormatted];
    return locationTimeString;
}
//北京时间  格式转换
- (NSDate *)beijingdate:(NSString *)createDate{
    NSDate *date;
    if ([createDate hasSuffix:@"Z"]) {
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        //输入格式
        [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SZ"];
        NSTimeZone *localTimeZone = [NSTimeZone localTimeZone];
        [dateFormatter setTimeZone:localTimeZone];
        
        date  = [dateFormatter dateFromString:createDate];
        
    }
    return date;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

遇见资源网 objective-c 关于NSDate时间的基础使用 http://www.ox520.com/16668.html

常见问题

相关文章

发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务