Converting NSString to NSDate (and back again)

后端 未结 17 2474
-上瘾入骨i
-上瘾入骨i 2020-11-21 23:26

How would I convert an NSString like \"01/02/10\" (meaning 1st February 2010) into an NSDate? And how could I turn the NSDat

相关标签:
17条回答
  • 2020-11-21 23:54

    using "10" for representing a year is not good, because it can be 1910, 1810, etc. You probably should use 4 digits for that.

    If you can change the date to something like

    yyyymmdd
    

    Then you can use:

    // Convert string to date object
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"yyyyMMdd"];
    NSDate *date = [dateFormat dateFromString:dateStr];  
    
    // Convert date object to desired output format
    [dateFormat setDateFormat:@"EEEE MMMM d, YYYY"];
    dateStr = [dateFormat stringFromDate:date];  
    [dateFormat release];
    
    0 讨论(0)
  • 2020-11-21 23:54

    NSString to NSDate or NSDate to NSString

    //This method is used to get NSDate from string 
    //Pass the date formate ex-"dd-MM-yyyy hh:mm a"
    + (NSDate*)getDateFromString:(NSString *)dateString withFormate:(NSString *)formate  {
    
        // Converted date from date string
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]];
        [dateFormatter setDateFormat:formate];
        NSDate *convertedDate         = [dateFormatter dateFromString:dateString];
        return convertedDate;
    }
    
    //This method is used to get the NSString for NSDate
    //Pass the date formate ex-"dd-MM-yyyy hh:mm a"
    + (NSString *)getDateStringFromDate:(NSDate *)date withFormate:(NSString *)formate {
    
        // Converted date from date string
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        //[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]];
        [dateFormatter setDateFormat:formate];
        NSString *convertedDate         = [dateFormatter stringFromDate:date];
        return convertedDate;
    }
    
    0 讨论(0)
  • 2020-11-21 23:55

    Best practice is to build yourself a general class where you put all your general use methods, methods useful in almost all projects and there add the code suggested by @Pavan as:

    + (NSDate *)getDateOutOfString:(NSString *)passedString andDateFormat:(NSString *)dateFormat{
    
        NSString *dateString = passedString;
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:dateFormat];
        NSDate *dateFromString = [[NSDate alloc] init];
        dateFromString = [dateFormatter dateFromString:dateString];
        return dateFromString;
    
    }
    

    .. and so on for all other useful methods

    By doing so you start building a clean reusable code for you app. Cheers!

    0 讨论(0)
  • 2020-11-21 23:59

    The above examples aren't simply written for Swift 3.0+

    Update - Swift 3.0+ - Convert Date To String

    let date = Date() // insert your date data here
    var dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "yyyy-MM-dd" // add custom format if you'd like 
    var dateString = dateFormatter.string(from: date)
    
    0 讨论(0)
  • 2020-11-22 00:01

    You can use extensions for this.

    extension NSDate {
        //NSString to NSDate
        convenience
        init(dateString:String) {
            let nsDateFormatter = NSDateFormatter()
            nsDateFormatter.dateFormat = "yyyy-MM-dd hh:mm:ss"
            // Add the locale if required here
            let dateObj = nsDateFormatter.dateFromString(dateString)
            self.init(timeInterval:0, sinceDate:dateObj!)
        }
    
        //NSDate to time string
        func getTime() -> String {
            let timeFormatter = NSDateFormatter()
            timeFormatter.dateFormat = "hh:mm"
            //Can also set the default styles for date or time using .timeStyle or .dateStyle
            return timeFormatter.stringFromDate(self)
        }
    
        //NSDate to date string
        func getDate() -> String {
            let dateFormatter = NSDateFormatter()
            dateFormatter.dateFormat = "dd, MMM"
            return dateFormatter.stringFromDate(self)
        }
    
        //NSDate to String
        func getString() -> String {
            let dateFormatter = NSDateFormatter()
            dateFormatter.dateFormat = "yyyy-MM-dd hh:mm:ss"
            return dateFormatter.stringFromDate(self)
        }
    }
    

    So while execution actual code will look like follows

        var dateObjFromString = NSDate(dateString: cutDateTime)
        var dateString = dateObjFromString.getDate()
        var timeString = dateObjFromString.getTime()
        var stringFromDate = dateObjFromString.getString()
    

    There are some defaults methods as well but I guess it might not work for the format you have given from documentation

        -dateFromString(_:)
        -stringFromDate(_:)
        -localizedStringFromDate(_ date: NSDate,
                         dateStyle dateStyle: NSDateFormatterStyle,
                         timeStyle timeStyle: NSDateFormatterStyle) -> String
    
    0 讨论(0)
  • 2020-11-22 00:02
    // Convert string to date 
    
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"yyyyMMdd"];
    NSDate *date = [dateFormat dateFromString:dateStr];  
    
    // Convert Date to string
    
    [dateFormat setDateFormat:@"EEEE MMMM d, YYYY"];
    dateStr = [dateFormat stringFromDate:date];  
    [dateFormat release];
    
    0 讨论(0)
提交回复
热议问题