I want convert from string to date - xcode

NSDate *LastBuildDate;
NSDate *PubDate;
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyyMMddhhmmss"];
LastBuildDate = [formatter dateFromString:buttoninfoamtion.mLastdate];
PubDate=[formatter dateFromString:newsItem.mPubDate];
if([PubDate compare:LastBuildDate])
buttoninfomation.mlastdate and newsItem.mPubDate is string from OpenAPI
ex)
buttoninfomation.mlastdate: Tue, 18 Oct 2011 15:54:43 +0900
newspub:Tue, 18 Oct 2011 15:58:00 +0900
but
LastBuidDate = (null)
PubDate = (null)
I want to compare two factor .
So date- date or string- string but I think string - string is so complex.
what wrong up codes?

Well the format you've given of "yyyyMMddhhmmss" doesn't match "Tue, 18 Oct 2011 15:54:43 +0900" even slightly.
Try
EEE, dd MMM yyyy HH:mm:ss Z
as the format string - see the iOS documentation and the Unix TR#35 for more information.

Related

Swift 2: Parse RSS pubDate to NSDate object

I´m trying to convert a String (Date) from an RSS Feed ("pubDate") to an NSDate object in Swift 2.
let dateString:String = "Tue, 16 Aug 2016 15:27:33 +0000"
let dateFormatter = NSDateFormatter()
dateFormatter.dateStyle = NSDateFormatterStyle.ShortStyle
dateFormatter.dateFormat = "yyyy-MM-dd"
let date = dateFormatter.dateFromString(dateString)
print(date)
The problem is, that in that case the date object is always nil. The problem is the format of the input string. If I change dateString to "2016-08-16" then the NSDate object is built correctly.
So I´ve no idea how to get this NSDate working. String operations on the dateString before? Or some options on the dateFormatter?
Thanks for you help in advance!
While questions about how to convert from string to date is mostly about specifying the right format string, you should read what Apple has to say about NSDateFormatter and Internet Dates. The short version is that you should always specify the local as en_US_POSIX:
let dateString = "Tue, 16 Aug 2016 15:27:33 +0000"
let dateFormatter = NSDateFormatter()
dateFormatter.locale = NSLocale(localeIdentifier: "en_US_POSIX")
dateFormatter.dateFormat = "E, d MMM yyyy HH:mm:ss Z"
let date = dateFormatter.dateFromString(dateString)
print(date)

String from Date works, Date from String doesn't work

I'm taking a NSDate and turning it into a dateString. But then...
When I try to take that same dateString and turn it back into a NSDate my dates aren't correct.
NSDate to dateString...
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "MMM d, yyyy H:mm a"
let dateString = dateFormatter.stringFromDate(timeDate as! NSDate)
print("Date: \(dateString)")
Console:
Date: Mar 22, 2016 22:30 PM
Date: Mar 23, 2016 1:00 AM
Date: Mar 23, 2016 9:00 AM
dateString to plainDate...
let reverseDateFormatter = NSDateFormatter()
reverseDateFormatter.dateFormat = "MMM d, yyyy H:mm a"
let plainDate = reverseDateFormatter.dateFromString(dateString)
print("Date Reverse: \(plainDate)")
Console:
Date Reverse: Optional(2016-03-22 17:30:00 +0000)
Date Reverse: Optional(2016-03-23 05:00:00 +0000)
Date Reverse: Optional(2016-03-23 05:00:00 +0000)
When your converting the Date to a string, your losing the timezone information, and then its not able to piece it back together.
Is there some reason you can't store the Date itself as an NSDate and then use that instead of re-building the date from a string?
If you need to convert it back to the full date from the string, you will need to adjust your format to store the timezone as well
stringFromDate returns a non-optional string, but dateFromString returns an optional date. Conditionally unwrap the result before printing it out:
if let result = reverseDateFormatter.dateFromString(dateString) {
print(result)
}

How to convert string to NSDate that has extraneous characters (swift)?

I have the string "Sat, 21 Nov 2015 19:20:48 EST\n\t\t\t" that I need convert to NSDate, but it gives me nil all the time.
I tried to clear the string by
dateString.stringByReplacingOccurrencesOfString("\"", withString: "", options: NSStringCompareOptions.LiteralSearch, range: nil)
result looks fine: Sat, 21 Nov 2015 19:20:48. Converting to NSDate = nil :(
Ok, I removed the noised characters manually and try to convert the string "Sat, 21 Nov 2015 19:20:48 EST" to NSDate. Nil again:
let dateString = "Sat, 21 Nov 2015 19:20:48 EST"
let formatter = NSDateFormatter()
formatter.locale = NSLocale(localeIdentifier: 'US_en')
formatter.dateFormat = 'yyyy-MM-dd'
print("date = \(date)")
//date = nil
What am I doing wrong?
Even if I use "formatter.timeZone = NSTimeZone(abbreviation: "EST")" still nil.
Answer to your first question:
You could use the following code to trim the unwanted whitespace and newlines from the string
var dateAsString = "Sat, 21 Nov 2015 19:20:48 EST\n\t\t"
dateAsString = dateAsString.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())
Answer to your second question:
Refer to the Date format symbols table in this link for list of all string formatting symbols.
You should be able to convert the specified date with this formatter,
let customDateFormatter = NSDateFormatter()
dayTimePeriodFormatter.dateFormat = "EEE, dd MMM yyyy HH:mm:ss zzz"
let date = dayTimePeriodFormatter.dateFromString(dateAsString) //"Sat, 21 Nov 2015 19:20:48 EST"

DateFromString returns wrong value

I'm using NSDateFormatter and datefromString is a whole year off
NSString *date = [[command substringWithRange:NSMakeRange(pos.location + 3, command.length - pos.location - 9)] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSLog(#" date [%#] " , date );
NSLog(#" lastAccess [%#] " , lastAccess );
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"YYYY-MM-dd HH:mm:ss Z"];
[dateFormat setTimeZone:[NSTimeZone systemTimeZone]];
NSDate *filedate = [dateFormat dateFromString:date];
NSLog(#"1 %# %f " , filedate , [filedate timeIntervalSince1970]);
NSLog(#"2 %# %f " , lastAccess , [lastAccess timeIntervalSince1970]);
NSLog(#"3 %# " , date);
produces the following output
date [2012-11-18 19:00:23 +0000]
lastAccess [2012-11-18 19:00:21 +0000]
1 2011-11-18 19:00:23 +0000 1321642823.000000
2 2012-11-18 19:00:21 +0000 1353265221.929860
3 2012-11-18 19:00:23 +0000
Date has 2012 which is confirm again with 3)
But check out 1) it has 2011
Yet 1) filedate datefromstring of date
Any ideas ?
Thanks in advance
use lower case letters for the year.
your dateformat should be yyyy-MM-dd HH:mm:ss Z.
according to the unicode date format guide
yyyy means Year
YYYY means Year in "Week of Year" based calendars.
I am not sure why this yields different results, but you should almost always use the lowercase variant.

NSDate odd behaviour

I'm trying to use +[NSDate dateWithNaturalLanguageString] on Mac OS X (Snow Leopard) to get tomorrow's date.
I was expecting that it would return tomorrow's date at 12AM but it returns tomorrow's date at 12PM. Is this a bug or am I doing something wrong?
Code:
date = [NSDate dateWithNaturalLanguageString:#"tomorrow"];
NSLog(#"DATE: %#", date);
Output: DATE: 2012-08-23 12:00:00 +0100
You need to use tomorrow at midnight instead tomorrow like this:
date = [NSDate dateWithNaturalLanguageString:#"tomorrow at midnight"];
NSLog(#"DATE: %#", date);
Output:
2012-08-24 00:00:00 +0100
Note:
You also can use Tomorrow at < time > AM if You want specific time, for example:
date = [NSDate dateWithNaturalLanguageString:#"tomorrow at 1:00 AM"];
or midnight:
date = [NSDate dateWithNaturalLanguageString:#"tomorrow at 12:00 AM"];

Resources