美文网首页
iOS 时间 时间戳

iOS 时间 时间戳

作者: yuezishenyou | 来源:发表于2017-11-06 16:23 被阅读0次

时间在线转化:http://tool.chinaz.com/Tools/unixtime.aspx

1.日期格式转字符串

//日期格式转字符串
- (NSString *)dateToString:(NSDate *)date withDateFormat:(NSString *)format
{

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    
    [dateFormatter setDateFormat:format];
    
    NSString *strDate = [dateFormatter stringFromDate:date];
    
    return strDate;
}

2.//字符串转日期格式

//字符串转日期格式
- (NSDate *)stringToDate:(NSString *)dateString withDateFormat:(NSString *)format
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    
    [dateFormatter setDateFormat:format];
    
    NSDate *date = [dateFormatter dateFromString:dateString];
    
    NSDate *mmm = [self worldTimeToChinaTime:date];
    
    return mmm;
}

//将世界时间转化为中国区时间
- (NSDate *)worldTimeToChinaTime:(NSDate *)date
{
    NSTimeZone *timeZone = [NSTimeZone systemTimeZone];
    
    NSInteger interval = [timeZone secondsFromGMTForDate:date];
    
    NSDate *localeDate = [date  dateByAddingTimeInterval:interval];
    
    return localeDate;
}

3.//date字符串转时间戳

//date字符串转时间戳
- (NSInteger)dateStrToTimeInterval:(NSString *)dataStr withDateFormat:(NSString *)format
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    
    [formatter setDateFormat:format];
    
    NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];
    
    [formatter setTimeZone:timeZone];
    
    NSDate* date = [formatter dateFromString:dataStr];
    
    //时间转时间戳的方法:
    
    NSInteger timeSp = [[NSNumber numberWithDouble:[date timeIntervalSince1970]] integerValue];
    
    return timeSp;
}

举例:

    
    NSDate *nowDate = [NSDate date];
    
    NSLog(@"----初始日期:%@-----",nowDate);

    NSString *str1 = [self dateToString:nowDate withDateFormat:@"yyyy-MM-dd HH:mm:ss"];

    NSLog(@"----当前日期:%@-----",str1);
    
    NSDate *date = [self stringToDate:str1 withDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    
    NSLog(@"----字符日期:%@-----",date);

    NSInteger inter = [self dateStrToTimeInterval:str1 withDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    
    NSLog(@"-----时间戳:%ld-----",inter);

相关文章

  • iOS 时间 时间戳

    时间在线转化:http://tool.chinaz.com/Tools/unixtime.aspx 1.日期格式转...

  • iOS 时间戳

    引言 iOS开发过程中会遇到很多时间转的问题。例如:当服务器端给我们一个时间戳的时候,我们需要把它转化成具体的时间...

  • iOS 时间戳

    背景: 在App的开发中,会遇到一些要计算时间差的问题,比如:在友盟统计的列子里面,要统计一个页面从创建到销毁的时...

  • iOS 时间戳

    NSDate 1.NSDate对象用来表示一个具体的时间点; 2.NSDate是一个类簇。我们所使用的NSDate...

  • iOS 获取时间字符串与时间戳

    获取当前时间 获取当前时间戳 时间戳转时间 字符串转时间戳 注意:iOS 返回的时间戳单位是秒。

  • iOS 时间戳、时间转换

    参考文档1:iOS时间类型转换和各种数据类型进行转换 参考文档2:iOS开发中的时间与日期(上) 转换工具:时间戳...

  • iOS时间戳转时间

    #pragma mark 时间戳转时间 - (NSString *)dateStr:(NSString *)tim...

  • 时间戳转时间 - iOS

    //时间戳转时间+ (NSString)getDateStringWithDate:(NSString)dataStr{

  • iOS时间戳转时间

    NSString*orderDate = @"1489373937000"; [self timestampSwi...

  • (IOS)时间戳转时间

    /**1.时间戳转换成时间*/ + (NSString*)dateWithString:(NSString*)st...

网友评论

      本文标题:iOS 时间 时间戳

      本文链接:https://www.haomeiwen.com/subject/oxvlmxtx.html