获取时间戳格式的当前NSDate

Sup*_*off 83 cocoa cocoa-touch nsdate nsdateformatter unix-timestamp

我有一个基本方法获取当前时间并将其设置为字符串.但是,如何在UNIX-1970时间戳格式的UNIX中格式化当前日期和时间?

这是我的代码:

NSDate *currentTime = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"hh-mm"];
NSString *resultString = [dateFormatter stringFromDate: currentTime];
Run Code Online (Sandbox Code Playgroud)

是否可以使用NSDateFormatter将'resultString'更改为时间戳?

Log*_*gan 216

这是我使用的:

NSString * timestamp = [NSString stringWithFormat:@"%f",[[NSDate date] timeIntervalSince1970] * 1000];
Run Code Online (Sandbox Code Playgroud)

(乘以1000毫秒,否则,取出来)

如果你一直在使用它,那么声明一个宏可能会很好

#define TimeStamp [NSString stringWithFormat:@"%f",[[NSDate date] timeIntervalSince1970] * 1000]
Run Code Online (Sandbox Code Playgroud)

然后像这样称呼它:

NSString * timestamp = TimeStamp;
Run Code Online (Sandbox Code Playgroud)

或者作为一种方法:

- (NSString *) timeStamp {
    return [NSString stringWithFormat:@"%f",[[NSDate date] timeIntervalSince1970] * 1000];
}
Run Code Online (Sandbox Code Playgroud)

作为TimeInterval

- (NSTimeInterval) timeStamp {
    return [[NSDate date] timeIntervalSince1970] * 1000;
}
Run Code Online (Sandbox Code Playgroud)

注意:

1000是将时间戳转换为毫秒.如果您希望以秒为单位的timeInterval,则可以删除它.

迅速

如果你想在Swift中使用全局变量,可以使用:

var Timestamp: String {
    return "\(NSDate().timeIntervalSince1970 * 1000)"
}
Run Code Online (Sandbox Code Playgroud)

然后,你可以打电话给它

println("Timestamp: \(Timestamp)")
Run Code Online (Sandbox Code Playgroud)

再次,这*1000是毫秒,如果你愿意,你可以删除它.如果你想保持它NSTimeInterval

var Timestamp: NSTimeInterval {
    return NSDate().timeIntervalSince1970 * 1000
}
Run Code Online (Sandbox Code Playgroud)

在任何类的上下文之外声明这些,并且它们可以在任何地方访问.

  • 谢谢@Logan,但我很确定宏总是气馁.你很容易忘记用宏来理解一个大程序.最好只创建一个执行此操作的方法,并在需要时调用它. (3认同)
  • 没问题,我用我更新的宏更新,以防它对你的情况有帮助! (2认同)

sag*_*444 15

使用 [[NSDate date] timeIntervalSince1970]

  • `@([[NSDate date] timeIntervalSince1970]).stringValue` (8认同)

Vic*_*cky 7

- (void)GetCurrentTimeStamp
    {
        NSDateFormatter *objDateformat = [[NSDateFormatter alloc] init];
        [objDateformat setDateFormat:@"yyyy-MM-dd"];
        NSString    *strTime = [objDateformat stringFromDate:[NSDate date]];
        NSString    *strUTCTime = [self GetUTCDateTimeFromLocalTime:strTime];//You can pass your date but be carefull about your date format of NSDateFormatter.
        NSDate *objUTCDate  = [objDateformat dateFromString:strUTCTime];
        long long milliseconds = (long long)([objUTCDate timeIntervalSince1970] * 1000.0);

        NSString *strTimeStamp = [NSString stringWithFormat:@"%lld",milliseconds];
NSLog(@"The Timestamp is = %@",strTimeStamp);
    }

 - (NSString *) GetUTCDateTimeFromLocalTime:(NSString *)IN_strLocalTime
    {
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"yyyy-MM-dd"];
        NSDate  *objDate    = [dateFormatter dateFromString:IN_strLocalTime];
        [dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
        NSString *strDateTime   = [dateFormatter stringFromDate:objDate];
        return strDateTime;
    }
Run Code Online (Sandbox Code Playgroud)

注意: - 时间戳必须是UTC区域,因此我将本地时间转换为UTC时间.


Pel*_*let 6

如果您想直接在NSDate对象上调用此方法并将时间戳作为字符串(以毫秒为单位)而没有任何小数位,请将此方法定义为类别:

@implementation NSDate (MyExtensions)
- (NSString *)unixTimestampInMilliseconds
{
     return [NSString stringWithFormat:@"%.0f", [self timeIntervalSince1970] * 1000];
}
Run Code Online (Sandbox Code Playgroud)