获取时间戳格式的当前NSDate

我有一个基本的方法获取当前时间,并将其设置为一个string。 然而,我怎样才能得到它格式化当前date和时间在一个UNIX自1970年以来的时间戳格式?

这是我的代码:

NSDate *currentTime = [NSDate date]; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"hh-mm"]; NSString *resultString = [dateFormatter stringFromDate: currentTime]; 

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

以下是我使用的:

 NSString * timestamp = [NSString stringWithFormat:@"%f",[[NSDate date] timeIntervalSince1970] * 1000]; 

(时间1000毫秒,否则取出)

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

 #define TimeStamp [NSString stringWithFormat:@"%f",[[NSDate date] timeIntervalSince1970] * 1000] 

然后像这样调用它:

 NSString * timestamp = TimeStamp; 

或者作为一种方法:

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

作为TimeInterval

 - (NSTimeInterval) timeStamp { return [[NSDate date] timeIntervalSince1970] * 1000; } 

注意:

1000是将时间戳转换为毫秒。 你可以删除这个,如果你喜欢你的timeInterval在几秒钟内。

迅速

如果你想在Swift中使用全局variables,你可以使用这个:

 var Timestamp: String { return "\(NSDate().timeIntervalSince1970 * 1000)" } 

那么,你可以打电话给它

 println("Timestamp: \(Timestamp)") 

再次, *1000是毫秒,如果你愿意的话,你可以删除它。 如果你想保持它作为NSTimeInterval

 var Timestamp: NSTimeInterval { return NSDate().timeIntervalSince1970 * 1000 } 

在任何课程的上下文之外声明这些内容,并且可以在任何地方访问它们。

使用[[NSDate date] timeIntervalSince1970]

 - (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; } 

注意: – 时间戳必须在UTC区,所以我把我们的本地时间转换为UTC时间。

如果您想直接在NSDate对象上调用此方法,并以毫秒为单位获取时间戳,而不带任何小数位,请将此方法定义为一个类别:

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

//以下方法会在转换为毫秒后返回时间戳。 [返回STRING]

 - (NSString *) timeInMiliSeconds { NSDate *date = [NSDate date]; NSString * timeInMS = [NSString stringWithFormat:@"%lld", [@(floor([date timeIntervalSince1970] * 1000)) longLongValue]]; return timeInMS; } 

也可以使用

 @(time(nil)).stringValue); 

以秒为单位的时间戳。

定义获取当前时间戳的macros是很方便的

 class Constant { struct Time { let now = { round(NSDate().timeIntervalSince1970) } // seconds } } 

那么你可以使用let timestamp = Constant.Time.now()

迅速:

我有一个UILabel,它显示相机预览TimeStamp。

  var timeStampTimer : NSTimer? var dateEnabled: Bool? var timeEnabled: Bool? @IBOutlet weak var timeStampLabel: UILabel! override func viewDidLoad() { super.viewDidLoad() //Setting Initial Values to be false. dateEnabled = false timeEnabled = false } override func viewWillAppear(animated: Bool) { //Current Date and Time on Preview View timeStampLabel.text = timeStamp self.timeStampTimer = NSTimer.scheduledTimerWithTimeInterval(1.0,target: self, selector: Selector("updateCurrentDateAndTimeOnTimeStamperLabel"),userInfo: nil,repeats: true) } func updateCurrentDateAndTimeOnTimeStamperLabel() { //Every Second, it updates time. switch (dateEnabled, timeEnabled) { case (true?, true?): timeStampLabel.text = NSDateFormatter.localizedStringFromDate(NSDate(), dateStyle: .LongStyle, timeStyle: .MediumStyle) break; case (true?, false?): timeStampLabel.text = NSDateFormatter.localizedStringFromDate(NSDate(), dateStyle: .LongStyle, timeStyle: .NoStyle) break; case (false?, true?): timeStampLabel.text = NSDateFormatter.localizedStringFromDate(NSDate(), dateStyle: .NoStyle, timeStyle: .MediumStyle) break; case (false?, false?): timeStampLabel.text = NSDateFormatter.localizedStringFromDate(NSDate(), dateStyle: .NoStyle, timeStyle: .NoStyle) break; default: break; } } 

我正在设置一个Button来触发一个alertView。

 @IBAction func settingsButton(sender : AnyObject) { let cameraSettingsAlert = UIAlertController(title: NSLocalizedString("Please choose a course", comment: ""), message: NSLocalizedString("", comment: ""), preferredStyle: .ActionSheet) let timeStampOnAction = UIAlertAction(title: NSLocalizedString("Time Stamp on Photo", comment: ""), style: .Default) { action in self.dateEnabled = true self.timeEnabled = true } let timeStampOffAction = UIAlertAction(title: NSLocalizedString("TimeStamp Off", comment: ""), style: .Default) { action in self.dateEnabled = false self.timeEnabled = false } let dateOnlyAction = UIAlertAction(title: NSLocalizedString("Date Only", comment: ""), style: .Default) { action in self.dateEnabled = true self.timeEnabled = false } let timeOnlyAction = UIAlertAction(title: NSLocalizedString("Time Only", comment: ""), style: .Default) { action in self.dateEnabled = false self.timeEnabled = true } let cancel = UIAlertAction(title: NSLocalizedString("Cancel", comment: ""), style: .Cancel) { action in } cameraSettingsAlert.addAction(cancel) cameraSettingsAlert.addAction(timeStampOnAction) cameraSettingsAlert.addAction(timeStampOffAction) cameraSettingsAlert.addAction(dateOnlyAction) cameraSettingsAlert.addAction(timeOnlyAction) self.presentViewController(cameraSettingsAlert, animated: true, completion: nil) 

}

  NSDate *todaysDate = [NSDate new]; NSDateFormatter *formatter = [NSDateFormatter new]; [formatter setDateFormat:@"MM-dd-yyyy HH:mm:ss"]; NSString *strDateTime = [formatter stringFromDate:todaysDate]; NSString *strFileName = [NSString stringWithFormat:@"/Users/Shared/Recording_%@.mov",strDateTime]; NSLog(@"filename:%@",strFileName); 

日志将是:文件名:/ Users / Shared / Recording_06-28-2016 12:53:26.mov

如果你需要时间戳作为一个string。

 time_t result = time(NULL); NSString *timeStampString = [@(result) stringValue]; 

从NSDate Swift 3获取时间戳

 func getCurrentTimeStampWOMiliseconds(dateToConvert: NSDate) -> String { let objDateformat: DateFormatter = DateFormatter() objDateformat.dateFormat = "yyyy-MM-dd HH:mm:ss" let strTime: String = objDateformat.string(from: dateToConvert as Date) let objUTCDate: NSDate = objDateformat.date(from: strTime)! as NSDate let milliseconds: Int64 = Int64(objUTCDate.timeIntervalSince1970) let strTimeStamp: String = "\(milliseconds)" return strTimeStamp } 

使用

 let now = NSDate() let nowTimeStamp = self.getCurrentTimeStampWOMiliseconds(dateToConvert: now)