ios 中怎样获取 Unix时间戳

将 2015-03-24 16:30:38  转换成 unix时间戳(1427188766)  然后上传。
有毫秒, 微秒等精度, 你要看你们的应用场景来决定


 //获取当前时间
     NSTimeInterval time=[[NSDate date] timeIntervalSince1970];// (NSTimeInterval) time = 1427189152.313643
    long long int currentTime=(long long int)time;      //NSTimeInterval返回的是double类型
    NSString *tempTime = [NSString stringWithFormat:@"%lld000",tempTime];//这里在后面补000了
   NSLog(@"%@",tempTime);

po:1427188766000
---------------------

NSTimeInterval timeInterval = [[NSDate date] timeIntervalSince1970];
   
long long dTime = [[NSNumber numberWithDouble:timeInterval] longLongValue]; // double转为long long
   
NSString *tempTime = [NSString stringWithFormat:@"%llu",dTime]; // 输出long long
     NSLog(@"%@",tempTime);
po:1427188766
------------------------

static NSString *clientId(void) {
     long long now_timestamp = [[NSDate date] timeIntervalSince1970] * 1000;
     int r = arc4random() % 1000;
     return [NSString stringWithFormat:@"%lld%u", now_timestamp, r];
}

NSTimeInterval time1=[[NSDate date] timeIntervalSince1970];
   
long long now_timestamp = time1 * 1000;
   
int r = arc4random() % 1000;
    NSString *cTime = [NSString stringWithFormat:@"%lld%u", now_timestamp, r];

Printing description of time1:
(NSTimeInterval) time1 = 1427190151.2044249
Printing description of now_timestamp:
(long long) now_timestamp = 1427190151204
Printing description of r:
(int) r = 429
Printing description of cTime:
1427190151204429

%1000  是取一个 1000以内的整数随机数据, 然后加到后面去了.
防止在极短的时间内请求两次, 导致 now_timestamp  是一样的,  但加一个随机数,  这样就几乎不可能一样了


-------------------------

生成 一个可读的时间戳(yyyyMMddHHmmssSSS)
20150204141203    直接用时间戳看着简单, 但可读性不高, 且容易弄乱. 如果能和后端商量好的话, 建议用可读性高的 时间形式 来直接表示


+ (NSString *)getTimeStringForNow
{
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"yyyyMMddHHmmssSSS"]; // 设置日期格式带毫秒的 20150204 163706 082
    NSString *destString = [dateFormat stringFromDate:[NSDate date]];
   
    return destString;
}

年月日时分秒毫秒


-----------------------------------

[[ NSDate date ] timeIntervalSince1970 ] ????



@property ( readonly ) NSTimeInterval timeIntervalSince1970;
按住option,鼠标点击(xcode)





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值