iOS-时间戳

作者: cb6a1e2768d1 | 来源:发表于2015-06-14 20:53 被阅读878次
/**
 *  生成时间戳
 *
 *  @return 时间戳
 */
- (NSString *)timeSp
{
    NSDate *datenow = [NSDate date];
    NSTimeZone *zone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"];
    NSInteger interval = [zone secondsFromGMTForDate:datenow];
    NSDate *localeDate = [datenow  dateByAddingTimeInterval: interval];
    NSString *timeSp = [NSString stringWithFormat:@"%f", [localeDate timeIntervalSince1970]];
    return timeSp;
}
//时间
    NSDate *now = [NSDate date];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSUInteger unitFlags = NSCalendarUnitEra | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
    NSDateComponents *dateComponent = [calendar components:unitFlags fromDate:now];
    int year = [dateComponent year];
    int month = [dateComponent month];
    int day = [dateComponent day];
    int hour = [dateComponent hour];
    int minute = [dateComponent minute];
    int second = [dateComponent second];
//时间格式化
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    //设定时间格式,这里可以设置成自己需要的格式
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    //用[NSDate date]可以获取系统当前时间
    NSString *currentDateStr = [dateFormatter stringFromDate:[NSDate date]];

相关文章

  • iOS- 时间和时间戳及与字符串转化

    iOS- 时间和时间戳及与字符串转化 1.获取当前时间 2.获取当前时间戳 3.时间戳转时间 4.字符串转时间戳 ...

  • iOS-时间戳

  • iOS-获取当前时间和当前时间戳

    获取当前的时间 获取当前时间戳有两种方法(以秒为单位) 获取当前时间戳 (以毫秒为单位) end

  • iOS-标准时间与时间戳转换

    设置时间显示格式:NSString* timeStr = @"2011-01-26 17:40:50";NSDat...

  • 时间戳

    很多网站在发布版本之前,都会在请求地址加一个时间戳进行版本更新。这样是版本更新时避免之前用户浏览器的缓存影响。请求...

  • 时间戳

    一. 关于[NSDatedate] 的问题 NSDate*date=[NSDatedate]; NSLog(@"d...

  • 时间戳

    时间戳 ios 在webView中的网页中,时间戳使用时(js)格式要用/ 而不能是-如 应该使用2018/09...

  • 时间戳

    时间戳转换成距此刻多久

  • 时间戳

    // 获取当前时间戳 + (NSString *)getCurrentTime { NSDate *sendd...

  • 时间戳

    // 获取当前时间戳(以s为单位) var timestamp = Date.parse(new Date());...

网友评论

    本文标题:iOS-时间戳

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