How to Convert String Date to Nsdate

Converting a string to an NSDate

NSString *dateStr = @"20100223";

// 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];

Hope this will help you.

How can I convert string date to NSDate?

try this:

let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = /* find out and place date format from
* http://userguide.icu-project.org/formatparse/datetime
*/
let date = dateFormatter.dateFromString(/* your_date_string */)

For further query, check NSDateFormatter and DateFormatter classes of Foundation framework for Objective-C and Swift, respectively.

Swift 3 and later (Swift 4 included)

let dateFormatter = DateFormatter()
dateFormatter.dateFormat = /* date_format_you_want_in_string from
* http://userguide.icu-project.org/formatparse/datetime
*/
guard let date = dateFormatter.date(from: /* your_date_string */) else {
fatalError("ERROR: Date conversion failed due to mismatched format.")
}

// use date constant here

how to convert a date string into a NSDate type swift

It is way easier for both you and the user if you let the user choose a date from a UIDatePicker. This has a lot of advantages:

  • The user don't have to type in three text fields
  • You can get the selected date via the date property. You don't need all these formatters and stuff
  • You can set a maximum date that the user can select via the maximumDate property. This way you don't have to check if it is in the future manually. Just set maximumDate to now.
  • The date picker will use different formats depending on the user's locale. You don't need to worry about it at all.

Just remember to set the datePickerMode to .date!

As for the conversion to NSDate, you can do this easily by this expression:

datePicker.date as NSDate

If you insist on using three text fields, you can do this:

dateFormatter.date(from: dateString) as NSDate? // note that this produces an optional

Convert date string to NSDate

Your date format and your time string should be same. In your case it should be like,

 let formatterJsonDate = DateFormatter()
formatterJsonDate.dateFormat = "yyy-MM-dd HH:mm:ss"

print(formatterJsonDate.date(from: "2016-10-08 00:20:00"))

Or change dateformatter like,

  "yyyy-MM-dd HH:mm:ss.SSSSSS"

And hh should be capital for 24 - hour format!!!! like HH!

Convert string to date in Swift

  • Convert the ISO8601 string to date

      let isoDate = "2016-04-14T10:44:00+0000"

    let dateFormatter = DateFormatter()
    dateFormatter.locale = Locale(identifier: "en_US_POSIX") // set locale to reliable US_POSIX
    dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
    let date = dateFormatter.date(from:isoDate)!
  • Get the date components for year, month, day and hour from the date

      let calendar = Calendar.current
    let components = calendar.dateComponents([.year, .month, .day, .hour], from: date)
  • Finally create a new Date object and strip minutes and seconds

      let finalDate = calendar.date(from:components)

Consider also the convenience formatter ISO8601DateFormatter introduced in iOS 10 / macOS 10.12:

let isoDate = "2016-04-14T10:44:00+0000"

let dateFormatter = ISO8601DateFormatter()
let date = dateFormatter.date(from:isoDate)!

How can I convert string date to NSDate?

I think your date format is wrong. change "dd/MM/YYYY" with "YYYY-MM-dd HH:mm:s" and try ...

You have a date like "2016-03-09 00:00:00" .. so format should be "YYYY-MM-dd HH:mm:s"

for i in date {
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "YYYY-MM-dd HH:mm:s"
if let showDate = dateFormatter.dateFromString(i){
print("Date with Time: \(showDate)")
dateFormatter.dateFormat = "YYYY-MM-dd"
let resultString = dateFormatter.stringFromDate(showDate)
print("final result: \(resultString)")
}
}

Result:-

Date with Time: 2016-03-09 00:00:00 +0000
final result: 2016-03-09
Date with Time: 2016-03-20 00:00:00 +0000
final result: 2016-03-20

Converting string to date with NSDateFormatter

NSString *str = @"2015-05-08T09:44:25.343";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc]init];
[dateFormat setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ss.SSS'Z'"];// this string must match given string @"2015-05-08T09:44:25.343"

NSDate *date = [dateFormat dateFromString:str];

NSLog(@"%@",date);
[dateFormat setDateFormat:@"MM/dd/yyyy"];// this match the one you want to be
NSString *convertedString = [dateFormat stringFromDate:date];
NSLog(@"%@", convertedString);

Convert NSDate to String in iOS Swift

you get the detail information from Apple Dateformatter Document.If you want to set the dateformat for your dateString, see this link , the detail dateformat you can get here
for e.g , do like

let formatter = DateFormatter()
// initially set the format based on your datepicker date / server String
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"

let myString = formatter.string(from: Date()) // string purpose I add here
// convert your string to date
let yourDate = formatter.date(from: myString)
//then again set the date format whhich type of output you need
formatter.dateFormat = "dd-MMM-yyyy"
// again convert your date to string
let myStringDate = formatter.string(from: yourDate!)

print(myStringDate)

you get the output as

Sample Image



Related Topics



Leave a reply



Submit