简体   繁体   English

通过NSDate获取时间和日期

[英]get time and date by NSDate

how can I retrieve the date and time from a NSDate. 如何从NSDate中检索日期和时间。 I want to see them separately. 我想分开看他们。 thanks 谢谢

NSDate *localDate = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
dateFormatter.dateFormat = @"MM/dd/yy";

NSString *dateString = [dateFormatter stringFromDate: localDate];



NSDateFormatter *timeFormatter = [[NSDateFormatter alloc]init];
timeFormatter.dateFormat = @"HH:mm:ss";


NSString *dateString = [timeFormatter stringFromDate: localDate];

There are many many different ways to represent the date and time so check NSDateFormatter for more info. 有许多不同的方式来表示日期和时间,因此请查看NSDateFormatter以获取更多信息。

i would go with using NSDateComponents and NSCalendar if you really want to have the values for the minute, hour and the date ... Something like this: 如果你真的想拥有分钟,小时和日期的值,我会选择使用NSDateComponents和NSCalendar ......这样的事情:

NSDateComponents *components = [[NSCalendar currentCalendar] components:NSHourCalendarUnit | NSMinuteCalendarUnit | NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit
                                                                   fromDate:[NSDate date]];

then you can use the properties of the components to access the value of each components with component.hour, component.year, etc. 然后,您可以使用组件的属性来访问component.hour,component.year等每个组件的值。

Also please note that if you are going with the NSDateFormatter example - then you should remember that NSDateFormatter is a heavy object and you should really reuse it if possible ... 另请注意,如果您使用NSDateFormatter示例 - 那么您应该记住NSDateFormatter是一个重型对象,如果可能,您应该重用它...

I'd use the following over the other answers: 我会在其他答案中使用以下内容:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];

NSString *formattedDateString = [dateFormatter stringFromDate:yourDate];
[dateFormatter release];

The advantage it has is that it will set the formatting to what the user has set in their system settings. 它的优点是它将格式设置为用户在系统设置中设置的格式。 That way you don't have to worry about items like if the user prefers their short dates in the format 10/31/11 or 31/10/11. 这样您就不必担心用户喜欢10/31/11或31/10/11格式的短日期等项目。 See the documentation for a list of styles that are available. 请参阅文档以获取可用样式列表。

Swift 2.2 : Swift 2.2

let dateFormatter = NSDateFormatter()
dateFormatter.dateStyle = NSDateFormatterStyle.MediumStyle
dateFormatter.timeStyle = .NoStyle
let myDateString = dateFormatter.stringFromDate(myNSDate)

dateFormatter.dateStyle = NSDateFormatterStyle.NoStyle
dateFormatter.timeStyle = .ShortStyle
let myTimeString = dateFormatter.stringFromDate(myNSDate)

In Swift you can enjoy yourself with extension, for instance: Swift中,您可以享受扩展,例如:

// MARK: - NSDate extension -

extension NSDate {

    func toString() -> String {
        let dateFormatter = NSDateFormatter()
        dateFormatter.dateFormat = "dd MMMM yyyy - HH:mm"
        return dateFormatter.stringFromDate(self)
    }

    func getTime() -> String {
        let dateFormatter = NSDateFormatter()
        dateFormatter.dateFormat = "HH:mm"
        return dateFormatter.stringFromDate(self)
    }

    func getDate() -> String {
        let dateFormatter = NSDateFormatter()
        dateFormatter.dateFormat = "dd/MM/yyyy"
        return dateFormatter.stringFromDate(self)
    }
}

And then, anywhere in your code you can do: 然后,您可以在代码中的任何位置执行以下操作:

print("Time right now: \(NSDate().getTime())") 

SWIFT 4 version of @McCygnus answer SWIFT 4版@McCygnus回答

let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .medium
dateFormatter.timeStyle = .none
let myDateString = dateFormatter.string(from: Date())

dateFormatter.dateStyle = .none
dateFormatter.timeStyle = .short
let myTimeString = dateFormatter.string(from: Date())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM