Convert Nsdate to String in iOS Swift

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

Convert NSDate to String with a specific timezone in SWIFT

Xcode 8 beta • Swift 3.0

Your ISO8601 date format is basic hms without Z. You need to use "xxxx" for "+0000".

"+0000" means UTC time.

let dateString = "2015-09-04 22:15:54 +0000"

let dateFormatter = DateFormatter()
dateFormatter.calendar = Calendar(calendarIdentifier: .ISO8601)
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss xxxx"
dateFormatter.locale = Locale(localeIdentifier: "en_US_POSIX")
if let dateToBeSaved = dateFormatter.date(from: dateString) {
print(dateToBeSaved) // "2015-09-04 22:15:54 +0000"
}

If you need some reference to create your date format you can use this:

Sample Image

Convert NSDate to time string

I will accept an answer that allows me to cast an NSDate as a Date

Say as Date.

Convert NSDate to Date

result.date is an optional NSDate, so you can bridge it
to an optional Date:

result.date as Date?

Then use optional binding to safely unwrap it. In your case that
could be

guard let date = result.date as Date? else {
// date is nil, ignore this entry:
continue
}

You might also want to replace

let commnt = String(describing: result.commnt)

with

guard let commnt = result.commnt else {
// commnt is nil, ignore this entry:
continue
}

otherwise you'll get comment strings like Optional(My comment).

(Rule of thumb: String(describing: ...) is almost never what you
want, even if the compiler suggests it to make the code compile.)

Convert NSDate to NSString

How about...

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy"];

//Optionally for time zone conversions
[formatter setTimeZone:[NSTimeZone timeZoneWithName:@"..."]];

NSString *stringFromDate = [formatter stringFromDate:myNSDateInstance];

//unless ARC is active
[formatter release];

Swift 4.2 :

func stringFromDate(_ date: Date) -> String {
let formatter = DateFormatter()
formatter.dateFormat = "dd MMM yyyy HH:mm" //yyyy
return formatter.string(from: date)
}

IOS: Convert a NSDate object into a string to get the current time

You need to convert the date to a string and then add it to your output stream.

Use:

NSDate *currentTime = [NSDate date]; 
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:@"hh:mm:ss"];
NSString *timeString = [formatter stringFromDate:currentTime];

You can then send timeString in your output stream

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)!

Getting next date when trying to convert NSDate to String

This is not standard behaviour. This happen because of the time zone difference. Set time zone proper

Set the timezone.

formatter.timeZone = [NSTimeZone timeZoneWithAbbreviation: @"GMT"];


Related Topics



Leave a reply



Submit