我有一种方法可以用长整数格式创建时间戳 例如:1386752892
+ (NSNumber *)currentTimestampWithLongLongFormat
{
double timeStamp = ceil([[NSDate date] timeIntervalSince1970] * 1000);
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setGeneratesDecimalNumbers:false];
NSNumber *timeNumber = [NSNumber numberWithDouble:timeStamp];
NSString *timeString = [formatter stringFromNumber:timeNumber];
// NSTimeInterval is defined as double
return [NSNumber numberWithLongLong:[timeString longLongValue]];
}
但这会产生13位数字 例如:1386752811802
如何解决问题并生成正确的数字格式?
答案 0 :(得分:2)
int timestamp = [[NSDate date] timeIntervalSince1970];
答案 1 :(得分:0)
试试这个
/**
* @param nil
* @return current time in mili second
*
* Fetch the current time stamp
*/
-(NSString *)currentTimeStamp {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSTimeZone *gmt = [NSTimeZone timeZoneWithName:@"GMT"];
[dateFormatter setTimeZone:gmt];
NSString *timeStamp1 = [dateFormatter stringFromDate:[NSDate date]];
NSDate *curdate = [dateFormatter dateFromString:timeStamp1];
double unix_timestamp = [curdate timeIntervalSince1970];
NSString *timeStamp = [NSString stringWithFormat:@"%f",unix_timestamp*1000];
return timeStamp;
}
答案 2 :(得分:0)
+ (NSString*) dateFromString:(NSString*)aStr
{
NSDateFormatter *formater = [[NSDateFormatter alloc] init];
[formater setDateFormat:@"yyyy-MM-dd HH:mm"];
NSDate *date2 = [formater dateFromString:aStr];
[formater setDateFormat:@"d MMM,yyyy HH:mm"];
NSString *result = [formater stringFromDate:date2];
return result;
}
+ (NSString *)calculateTime:(NSString *)datetime :(NSString *)servertime
{
NSString *time;
NSDate *date1;
NSDate *date2;
{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
date1 = [formatter dateFromString:datetime];
date2 = [formatter dateFromString:servertime];
}
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *differenceComponents = [calendar components:(NSDayCalendarUnit)
fromDate:date1
toDate:date2
options:0];
NSTimeInterval interval = [date1 timeIntervalSinceDate: date2];//[date1 timeIntervalSince1970] - [date2 timeIntervalSince1970];
int hour = interval / 3600;
int minute = (int)interval % 3600 / 60;
int seconds = (int)interval % 60;
hour=ABS(hour);
minute=ABS(minute);
seconds=ABS(seconds);
if ([differenceComponents day]>0) {
time= [NSString stringWithFormat:@"%ld %@", (long)[differenceComponents day],[NSString stringWithFormat:NSLocalizedString(@"daysago", nil)]];
}
else
{
if ([differenceComponents day] == 0) {
time= [NSString stringWithFormat:@"%ld %@", (long)[differenceComponents day],[NSString stringWithFormat:NSLocalizedString(@"dayago", nil)]];
if (hour>0) {
time= [NSString stringWithFormat:@"%d %@", ABS(hour),[NSString stringWithFormat:NSLocalizedString(@"hourago", nil)]];
}
else {
time= [NSString stringWithFormat:@"%d %@", ABS(hour),[NSString stringWithFormat:NSLocalizedString(@"hoursago", nil)]];
if (minute>0) {
time= [NSString stringWithFormat:@"%d %@", ABS(minute),[NSString stringWithFormat:NSLocalizedString(@"minuteago", nil)]];
}
else {
time= [NSString stringWithFormat:@"%d %@", ABS(minute),[NSString stringWithFormat:NSLocalizedString(@"minuteago", nil)]];
if (seconds>0) {
time= [NSString stringWithFormat:@"%d %@", ABS(seconds),[NSString stringWithFormat:NSLocalizedString(@"secondago", nil)]];
}
else {
time= [NSString stringWithFormat:@"%d %@", ABS(seconds),[NSString stringWithFormat:NSLocalizedString(@"secondsago", nil)]];
}
}
}
}
}
return time;
}
///根据要求,我们将使用日期格式