将 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)