iOS获取网络时间;

//获取网络时间

+ (NSString *)getTimeLine

{

    NSString *urlString = @"http://m.baidu.com";

    urlString = [urlString stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];

    [request setURL:[NSURL URLWithString: urlString]];

    [request setCachePolicy:NSURLRequestReloadIgnoringCacheData];

    [request setTimeoutInterval: 2];

    [request setHTTPShouldHandleCookies:FALSE];

    [request setHTTPMethod:@"GET"];

    NSHTTPURLResponse *response;

    [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];

    NSString *date = [[response allHeaderFields] objectForKey:@"Date"];

    date = [date substringFromIndex:5];

    date = [date substringToIndex:[date length]-4];

    NSDateFormatter *dMatter = [[NSDateFormatter alloc] init];

    dMatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];

    [dMatter setDateFormat:@"dd MMM yyyy HH:mm:ss"];

    NSDate *netDate = [dMatter dateFromString:date];

    NSTimeZone *zone = [NSTimeZone systemTimeZone];

    NSInteger interval = [zone secondsFromGMTForDate: netDate];

    //当前时间,时分秒

    NSDate *localeDate = [netDate  dateByAddingTimeInterval: interval];

    //再转换成毫秒

    NSTimeInterval logDateInterval= [localeDate timeIntervalSince1970];

    long long logTime= logDateInterval*1000;

    //默认是秒,毫秒  时间戳*1000 ,想取得微秒  时间戳 * 1000 * 1000

     return [NSString stringWithFormat:@"%lld", logTime];

}


//获取本地时间;

+ (NSString *)getTimeLine

{

    return [NSString stringWithFormat:@"%lld", (long long)([[NSDate date]timeIntervalSince1970] * 1000)];

}









评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值