0

I seem to be stuck here and I have been wasting way too much tome on this.

What I have is a string that is in the RFC 1123 format that I would like to get a date out of, but not matter what I do, I get a nil result;

 let dateFormat = NSDateFormatter();

    dateFormat.dateStyle = .MediumStyle;
    dateFormat.dateFormat = "EEE',' dd MMM yyyy HH':'mm':'ss z";
    dateFormat.locale = NSLocale.systemLocale();
    dateFormat.timeZone = NSTimeZone(abbreviation:"GMT");
    var currentDate = dateFormat.dateFromString("Sun, 28 Jun 2015 04:30:54 GMT");

I am not sure what I am missing, if I changed the MMM to MM and make Jun 06, then it works. It seems to be only this instance. I have tried moving the order of how dateFormat gets created, and still I get no results. Any help on this matter would greatly be appreciated

4

1 に答える 1

2

フォーマッタを混乱させたと思います。フォーマッタの仕事は、読み取った文字列から他の設定を学習することなので、書式文字列以外は何も設定する必要はありません。

let dateFormat = NSDateFormatter()
dateFormat.dateFormat = "EEE',' dd MMM yyyy HH':'mm':'ss z"
var currentDate = dateFormat.dateFromString("Sun, 28 Jun 2015 04:30:54 GMT")

// "Jun 27, 2015, 11:30 PM"

上記のようにすると、NSDate?指定した日付文字列から が返されます。

于 2015-06-28T04:58:26.240 に答える