iOS Nsdate Comparison Works Differently When the 24-Hour Time in Date Settings Toggles Between on and Off

iOS NSDate Comparison works differently when the 24-Hour Time in date settings toggles between ON and OFF?

See Apple's Technical Q&A 1480.

You need to set the date formatter's locale to the special locale of en_US_POSIX. You also need to specify a 24-hour hour format - HH, not hh.

-(NSDate *)getDateFromString:(NSString *)dateString{
NSLocale *posix = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
NSDateFormatter* fmt = [[NSDateFormatter alloc] init];
[fmt setLocale:posix];
[fmt setDateFormat:@"dd MMM yyyy HH:mm:ss"];

return [fmt dateFromString:dateString];
}

App Crashed when device time is in 24 hour format

@Rajan Thanks for giving me idea of NSLocale. I set the dateformatter's locale identifier to "en_US_POSIX".
i just add this below line in my code after allocating date formatter. dateformatter.locale = NSLocale(localeIdentifier:"en_US_POSIX")
Thanks frnd for giving this idea

On hour is subtracted when converting a String to NSDate

@mipadi has explained really well the problem, so I think that you should just set the time zone to UTC.

    let dateString = "2015-07-13T17:32:32.781Z"
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "YYYY-MM-dd'T'HH:mm:ss.SSS'Z'"
dateFormatter.timeZone = NSTimeZone(abbreviation: "UTC")
var theDate = dateFormatter.dateFromString(dateString)!
println(theDate)

returns a date an hour in the future

Maybe you are confusing the point in time (ie the NSDate object) and the point in time at your location (ie your local time).

If you print a NSDate (like NSLog(@"%@", [NSDate date]); which invokes [date description]) the date representation that is printed is in UTC timezone (+0000) (at least it is on my computer).

So as long as you don't live in an area that uses UTC the date printed by [date description]; is always "wrong". But wrong only means that its representation is not the same representation as the clock in your office. The date (as in point in time) is still correct.

When you use localizedStringFromDate:dateStyle:timeStyle: you are printing the date in your local timezone.

NSDate *date = [NSDate date];
NSLog(@"%@", date);
NSLog(@"%@", [NSDateFormatter localizedStringFromDate:date dateStyle:NSDateFormatterShortStyle timeStyle:NSDateFormatterShortStyle]);

at my computer this results in:

2011-02-12 08:32:10.658 x[75647:207] Date: 2011-02-12 07:32:10 +0000
2011-02-12 08:32:10.661 x[75647:207] Date: Saturday, February 12, 2011 8:32:10 AM Central European Time

the printed strings are different, but the NSDate object is still the same. That's why you have to use NSDateFormatters when you show a date to the user. Because the same point in time looks different on different places of the world.


But there are only three places where an UTC formatted date would be one hour in the future, so if you don't live in greenland, cape verde or on the azores I might be totally wrong and there is something wrong with your NSDate objects.


Edit: Out of curiosity I read the documentation about [date description] again. And it says

A string representation of the
receiver in the international format
YYYY-MM-DD HH:MM:SS ±HHMM, where ±HHMM
represents the time zone offset in
hours and minutes from GMT (for
example, “2001-03-24 10:45:32 +0600”).

So I don't know why the date at my computer is printed in GMT timezone. It might be in another timezone at your computer.

But still, it's only the representation, the date is still the same.

Time Format Contains Colon(:) Not supported by iOS 12.2

Just change date format h:MM a to hh:mm a

Please refer below code.

static func getDateInWithoutTimeZone(format: String, from timeStamp: Int64) -> String? {
let date = Date(timeIntervalSince1970: TimeInterval(timeStamp))
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.dateFormat = format //Specify your format that you want
return dateFormatter.string(from: date)
}

guard let time = getDateInWithoutTimeZone(format: "hh:mm a", from: 1555395758) else {
return
}
print(time)

Console Log

MM which stands for months, not minutes.



Related Topics



Leave a reply



Submit