How to Use Nsdateformatter to Venezuela

How to handle different date time formats using NSDateFormatter

A date formatter can only handle one format at a time. You need to take this approach:

NSDateFormatter *f = [[NSDateFormatter alloc] init];
[f setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *date = [f dateFromString:@"2010-01-10 13:55:15"];

NSDateFormatter *f2 = [[NSDateFormatter alloc] init];
[f2 setDateFormat:@"d. MMMM YYYY"];
NSString *s = [f2 stringFromDate:date];

s will now be "10. January 2010"

iPhone NSDateFormatter Timezone Conversion

Honestly, you'll just have to change the source data (removing the colon) before running it through the formatter. Your original date string is non-standard and none of the time zone format strings will work properly on it.

You can see the valid inputs on unicode.org.

ZZZ e.g. "-0500"

ZZZZ e.g. "GMT-05:00"

Nothing for "-05:00"

NSDateFormatter timezone (Etc/GMT) not getting parsed for dateFromString

NSString *dateString=@"2011-07-11 13:28:59 Etc/GMT";
dateString = [dateString stringByReplacingOccurrencesOfString:@"Etc/GMT" withString:@"GMT"];
NSDateFormatter *dateformatter=[[NSDateFormatter alloc] init];
[dateformatter setDateFormat:@"yyyy-MM-dd HH:mm:ss z"];

// Or stuff....>>

NSDate *date = [dateformatter dateFromString: dateString];
NSLog(@"You may Need This =========%@",date);
dateformatter = [[[NSDateFormatter alloc] init] autorelease];
[dateformatter setDateFormat:@"yyyy-MM-dd hh:mm:ss a"];

NSString *convertedString = [dateformatter stringFromDate:date];
NSLog(@"Converted String : %@",convertedString);

/// may help =====>> http://unicode.org/reports/tr35/

Why does my NSDateFormatter sometimes return an a.m. or p.m. with yyyyMMddHHmmssSSS?

The problem you are describing is a known bug. Check out some discussion on the problem on stackoverflow, and you can find some possible work-arounds there.

Here's an excerpt of huyz's explaination of the bug:

The problem comes from NSDateFormatter somehow “getting stuck” in the 12 or 24-hour time mode that the user has manually selected. So if a French user manually selects 12-hour mode, and the application requested NSDateFormatter to output time with the 24-hour format “HHmm”, it would actually receive time in a 12-hour format, e.g. “01:00 PM”, as if the application had instead requested “hhmm aa”. The reverse would happen if a US user manually selected 24-hour mode: outputting time with the 12-hour format “hhmm aa” would actually get you time in the 24-hour format instead, e.g. “17:00″.

Setting UIDatePicker date with NSDateFormatter

You're missing the Z format specifier in the date format.

This code:

NSString *calDate = @"2011-11-11 21:56:38 +0000";
NSDate *date = [[[NSDate alloc] init] retain];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];

[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss Z"];
// right here ^

date = [dateFormatter dateFromString:calDate];

NSLog(@"date:%@", date);

Outputs:

2012-01-03 20:14:15.258 Craplet[38753:707] date:2011-11-11 21:56:38 +0000

Without the Z:

[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

It outputs:

2012-01-03 20:16:10.456 Craplet[38885:707] date:(null)

If the format cannot be matched to the date string, it returns nil

iOS NSDate and NSDateFormatter behavior

As pointed out in the comments on your question, your NSDate is correct; NSDate always stores its time in GMT. By default, when NSDateFormatter parses your date it converts it from your local time zone to GMT so that what's stored in the resulting NSDate object is consistent (it's actually more nuanced than that, but you can think of it that way). Since your time zone is GMT-04:30 that means it's going to add 4 hours and 30 minutes to your time, making 2014-09-04 12:34:21 GMT-4:30 = 2014-09-04 17:04:21 GMT (as @MartinR pointed out).

From your comments, it sounds like you need to store this date in your database in the format yyyy-MM-dd HH:mm:ss, in local time. The easiest way to do that is to simply use a second NSDateFormatter:

NSDateFormatter *formatOut = [[NSDateFormatter alloc] init];
[formatOut setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSString *dateString = [formatOut stringFromDate: date];

Since NSDateFormatter will use your local time zone by default, that should "convert" date from GMT back to GMT-4:30 and set dateString to 2014-09-04 12:32:21.


All that being said, storing a date in your local time zone is almost always a bad idea. Time zones are tricky things, so it's best to always store and do any calculations on your date in GMT/UTC, then only convert it to local time when you're displaying it to your user. This will save you a lot of pain if someone outside your time zone accesses your data, your time zone rules change (happens more often than you might think), you need to pass your date to some external service/app, etc.

xcode NSDateFormatter returning null inside configureView

I think NSDateFormatterMediumStyle corresponds to something like this - Nov 23, 1937. Hence you are getting null value.

Can you try this?

[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss Z"];

Issue with Caracas time zone in iOS

This is not a programming problem, Caracas(Venezuela) timezone has changed recently.

UTC-04:00 is correct right now.

Presidents of Venezuela had changed this a couple of times:

  • UTC-04:30 was used since 2007.
  • It was recently changed again to UTC-04:00.

http://www.bloomberg.com/news/articles/2016-04-14/maduro-orders-time-zone-change-to-battle-venezuela-power-crisis

Check whether a specified date is today, yesterday, or a future date

Try this:

Note: Change the date format as per your need.

NSDateFormatter* df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"MM/dd/yyyy"];
NSDate* enteredDate = [df dateFromString:@"10/04/2011"];
NSDate * today = [NSDate date];
NSComparisonResult result = [today compare:enteredDate];
switch (result)
{
case NSOrderedAscending:
NSLog(@"Future Date");
break;
case NSOrderedDescending:
NSLog(@"Earlier Date");
break;
case NSOrderedSame:
NSLog(@"Today/Null Date Passed"); //Not sure why This is case when null/wrong date is passed
break;
}


Related Topics



Leave a reply



Submit