DateFromString returns wrong value - xcode

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.

Related

Xamarin Form Receiving an Arabic DatePicker Binding Error

System.ArgumentOutOfRangeException: Specified time is not supported in this calendar. It should be between 04/30/1900 00:00:00 (Gregorian date) and 11/16/2077 23:59:59 (Gregorian date), inclusive.
Parameter name: time
because your ar-sa culture's default calender is UmAlQuraCalendar calender.Its range is
1318/01/01 - 1500/12/30 ,
so you should change the date to Gregorian date,
for example ,you get a date 1378/1/1
UmAlQuraCalendar umAlQuraCalendar = new UmAlQuraCalendar();
var datatime = new DateTime(1378,3,1, umAlQuraCalendar);
GregorianCalendar gregorian = new GregorianCalendar();
DateTime gregorianDate = new DateTime(gregorian.GetYear(datatime), gregorian.GetMonth(datatime), gregorian.GetDayOfMonth(datatime));
Console.WriteLine("ar-sa----" + gregorianDate .ToString());//the result will be:"ar-sa----9/14/1958 12:00:00 AM"

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

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

I want convert from string to date

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.

Resources