SketchK
SketchK

Reputation: 411

Swift show a different result with Objective-C in convert NSString to NSDate?

i use Objective-C to write some code which can help me to convert NSString to NSDate,but i find something interesting !!!

Here is my code:

NSString *string = @"周四 3月 24 14:44:00 +0800 2016";
NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
fmt.dateFormat = @"EEE MMM dd HH:mm:ss Z yyyy";
fmt.timeZone = [NSTimeZone timeZoneWithName:@"CCT"];
fmt.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh"];
NSDate *date = [fmt dateFromString:string];
NSLog(@"%@", date);

and the result is

2016-03-24 06:44:00 +0000

but i use swift to write the same thing

var string = "周四 3月 24 14:44:00 +0800 2016"
var fmt = NSDateFormatter();
fmt.dateFormat = "EEE MMM dd HH:mm:ss Z yyyy"
fmt.timeZone = NSTimeZone(name: "CCT")
fmt.locale = NSLocale(localeIdentifier:"zh")
fmt.dateFromString(string);

and the result is

"Mar 24, 2016, 2:44 PM"

you can find that swift gives us a correct time, but Objective-C seems incorrect.

so, what's wrong with the code?

Upvotes: 0

Views: 126

Answers (3)

Duncan C
Duncan C

Reputation: 131481

There are 2 parts to this. You have a date formatter that you are using to convert a date string to an NSDate.

Then you need some method to display that date as a string. If you use NSLog to display a date, it gets converted to a string by calling the object's description method.

Don't do that. Instead, feed the resulting date through a date formatter's stringFromDate method.

So you could use:

NSDate *date = [fmt dateFromString:string];
NSLog(@"date = %@", [fmt stringFromDate: date];

or from Swift:

if let date = fmt.dateFromString(string)
{
  print("date = /(fmt.stringFromDate(date))"
}

That will give the same results in both - the original date string. Alternately you could create a separate output date formatter that uses a different locale and/or format string and use that to display your results.

EDIT:

You can create a date formatter purely for debugging. Here's the Objective-C version:

- (NSDateFormatter *) debugDateFormatter;
{
  if (!_debugDateFormatter)
  {
    _debugDateFormatter = [[NSDateFormatter alloc] init];
    _debugDateFormatter.dateStyle = NSDateFormatterShortStyle;
    _debugDateFormatter.timeStyle = NSDateFormatterShortStyle;
  }
  return _debugDateFormatter;
  
}

Then you'd use:

NSDate *date = [fmt dateFromString:string];
NSLog(@"date = %@", [self.debugDateFormatter stringFromDate: date];

Or in Swift

class Foo: NSObject
{
  lazy var debugDateFormatter: NSDateFormatter =
  {
    () -> NSDateFormatter in
    let debugDateFormatter = NSDateFormatter()
    debugDateFormatter.dateStyle = .MediumStyle
    debugDateFormatter.timeStyle = .MediumStyle
    return debugDateFormatter
  }()
  
  func showDate(date: NSDate)
  {
    print( "date = \(debugDateFormatter.stringFromDate(date))")
  }
}

let aFoo = Foo()

aFoo.showDate(NSDate())

(In the code above I created a class Foo simply as a place to define my lazy NSDateFormatter debugDateFormatter. Your lazy var debugDateFormatter would probably go in whatever class needs it.)

Upvotes: 1

Felix
Felix

Reputation: 35394

The results are actually the same. Only the output formatting is different.

Change the swift code at the end to get the same result (so both use NSDate's description method under the hood):

let date = fmt.dateFromString(string)!
print(date)

Upvotes: 0

Moin Shirazi
Moin Shirazi

Reputation: 4425

Because your time zone is different in both

Upvotes: 0

Related Questions