I need to compare two time which is in the below format.
NSDate *dt1;
NSDate *dt2;
NSComparisonResult *cr = [dt1 compare:dt2];
the comparison doesnt consider the AM/PM and produces wrong results.
For Eg: If the time is 6.12 PM, then the results are correct during comparison. But if it is 6.12 AM it still considers as 6.12PM.
Please help me in fixing the issue related to AM/PM. Any help is appreciated.
Thanks
I think you're highly confused.
NSDate *dt1 = 4:30 AM;
is not even remotely close to valid syntax. What is your actual object?
I'm guessing here that you don't actually have 2 NSDate* objects, but rather have something else.
Related
When i call [NSDate date] it returns 2 hours less than it should. I've checked my computer clock settings and they are all ok. I've checked settings in iPhone and time zone and clock are all OK. Even simulator shows correct time on top toolbar. But when I try to log current date it shows 2 hours less than it should. I ran out of ideas where to look.
All dates returned by [NSDate date] are in the GMT time zone.
When you use any NSDateFormatter, just set the time zone and it will print out the correct time.
Look carefully at the output of the NSLog() statement. You will see that the output always contains the timezone using standard UTC. Therefore, the date is actually correct, taking into account the timezone.
I found my solution here: How to convert time to the time zone of the iPhone device?
So if you ever want to change [NSDate date] to point to local time just use the code provided in the link above. And dont forget to change timeZoneWithAbbreviation on sourceTimeZone from EST to GMT (because NSDate is always in GMT)
Once again thanks everyone for helping out..
I have an NSString like this #"2010-08-30T11:00:00-04:00" . How to convert this to an NSDate ? Which DateFormat should be used with it ? I tried this #"yyyy-MM-dd'T'HH:mm:ss-SSS" . But didn't worked. Please help me.
Edit
I found #"yyyy-MM-dd'T'HH:mm:ss-SSS" was working fine in OS version 3.1 . But its not getting in 4.0 .This question also pointing similar problem. Please give a solution
[yourNSDateFormatterVariable setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"GMT"]];
Knowing the timezone to be GMT - 4 hours is not enough to determine if daylight saving should be applied or not. Daylight saving is determined based on the geopolitical borders. For instance is daylight saving not applied in the same months above and below equator. Additional countries close to equator do seldom use daylight saving at all.
So, you need to get the named timezone in order to be able to apply daylight saving correctly.
Is there a good+easy way to convert an Oracle Timestamp to an NSDate object on the iPhone? I have asked my customer to give me a DB with timestamps as Unix Timestamp (doubles with 0 = start of 1970) but it seems to be a problem for them. Thanks.
Note: You can easily convert a Unix Timestamp to an NSDate with
[NSDate dateWithTimeIntervalSince1970: timestamp] // timestamp is a "double"
What I need to do is convert the Oracle timestamp to a double, aka Unix Timestamp. The rest is easy.
More info: I need Objective-C code to run on the iPhone using NSStrings that represent dates in Oracle Timestamp format. Another StackOverflow thread suggests using NSDateFormatter, but I don't know what format to use and the initialization method for the formatter suggested in that thread generates a warning for me.
I'm not sure about the converting to an NSDate, but you can convert an Oracle date to a unix timestamp easily. The following SQL snippet will do it:
(my_date - to_date('01/01/1970','DD/MM/YYYY')) * 86400
It looks like my best bet is to use the following:
NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"MM/dd/yyyy HH:mm a"];
I'm not sure how much variance there is in Oracle timestamps, so if there is a problem with the above approach, I'd love to know.
Thanks for the other answers. I will leave the question open to invite other approaches.
It's a shame that you accepted that answer already but this is the best way to do it:
NSDate *dateTraded = [NSDate dateWithTimeIntervalSince1970 :[timestampVal integerValue]];
...where timestampVal is an NSString object representing a timestamp value.
I have an array of NSDates which I build from strings using [NSDate dateFromString]
In the xml I parsed to get the string there was also a timezone string. As far as I can see in the manual NSDate does not in it self deal with timezones. Do I need to always store this timezone value somewhere and pair it with the belonging NSDate each time I need it?
I also need to figure out that if an event starts in London at 10:00, but I am in Denmark having my iPhone set to danish time my "event started in London" should display at 09:00 o'clock.
Again if an event starts in London at 10:00 o'clock and ends in Denmark at 12:00 o'clock, If I were to compare start time and end time using an iPhone with danish settings I would get that the duration of the event was 02:00 event though 10:00 o'clock in UK and 12:00 o'clock in Denmark is only 1 hour apart.
NSdate works really well for these things in the scope of one timezone, but introducing the timezone part just made everything complicated to me. Is there a way to abstract/hide all these calculations, as I see potential for making a lot of mistakes.
I have been through the NSDateformatter and NSDate guides from Apple, but they are really vague and sports a substantial amount of deprecated code :/
Thanks for any help given.
You should take one standard timezone like UTC/GMT format for all calculation.
According to the NSDate reference, dateWithString: takes an offset to GMT as last component; while it is not a time zone, it is sufficient to perform computation or comparison).
Looking at the NSTimeZone reference, you can use the abbreviationForDate: and the timeZoneWithAbbreviation: to get a NSTimeZone object from a NSDate instance. Once you get the time zone, you have everything you need.
I convert the present date and the date I would like to know if is close, to GMT and then returning the difference. So I changed every thing to deal with differences instead of actual times and dates. A bit like a music score transposed to a different key:)
+ (NSInteger) minutesUntilDate:(NSDate*) date withTimezoneOffset:(NSInteger) GMTOffset
{
NSDate *now = [NSDate date];
NSTimeInterval localTimeZoneOffset = [[NSTimeZone defaultTimeZone] secondsFromGMT];
now = [now addTimeInterval:(localTimeZoneOffset * -1)];
date = [date addTimeInterval:(GMTOffset * 60 * 60) * −1];
return ((NSInteger)[now timeIntervalSinceDate:date] / 60 ) * -1;
}
As soon as you have allocated an NSDate, these do not have timezone information any longer. NSDate is "timezone-less" and is always in GMT. You should make sure that NSDate understand your format correctly when allocating it.
Once you have an NSDate you can make normal calculations and ignore the timezones.
You only need to take care of timezones when reading strings into NSDates and when printing them out.
I am working on a project which involved in converting current time on iPhone to target destination's time. For example, the application needs to convert current time (08:00) to Germany's local time.
I have information about timezone (like UTC +1) and tried to search about how to use NSTimeZone to convert the NSDate value, but it looks like I have to ask your help here.
Could you give me any suggestion? or point me out for some solution?
The simplest thing is probably to use -[NSTimeZone secondsFromGMTForDate:] for each time zone, and then figure out the delta between the two:
NSInteger offset1 = [timezone1 secondsFromGMTForDate: date];
NSInteger offset2 = [timezone2 secondsFromGMTForDate: date];
return [date addTimeInterval: (offset1 - offset2);
Check out NSDateFormatter and its setTimeZone: method.
This guide on working with time zone differences from Apple might be a useful guide to follow.