get time and date by NSDate

2019-02-01 05:57发布

how can I retrieve the date and time from a NSDate. I want to see them separately. thanks

4条回答
聊天终结者
2楼-- · 2019-02-01 06:34

In Swift you can enjoy yourself with extension, for instance:

// 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())") 
查看更多
干净又极端
3楼-- · 2019-02-01 06:35

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 *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.

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 ...

查看更多
我想做一个坏孩纸
4楼-- · 2019-02-01 06:55
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.

查看更多
冷血范
5楼-- · 2019-02-01 06:56

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. See the documentation for a list of styles that are available.

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)
查看更多
登录 后发表回答