Setting up AM/PM for my time in iOS - ios

My original date format is : 2014-03-14T10:35:24.537
So I first separate the time and date with componentsSeparatedByString, then I save the second half (the time part) to NSString time, while eliminating the microseconds. to the format 10:35. I'm trying to get it to add PM/AM but setDateFormat:#"hh:mm a" is not doing it. What am I doing wrong?
NSArray *components = [datestr componentsSeparatedByString:#"T"];
NSString *time = components[1];
time = [time substringWithRange:NSMakeRange(0, 5)];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"hh:mm a"];
NSDate *timeFromString = [formatter dateFromString:time];
NSLog(#"%#", timeFromString);
When I log timeFromString, I get a null.
EDIT: I changed the formatter above to [formatter setDateFormat:#"hh:mm"]; and now the timeFromString logs as: 2000-01-01 18:35:00 +0000 when data coming in is 2014-03-14T10:35:28.42

A date formatter is used to convert dates to or from a string. A single date formatter cannot be used to convert between two different date formats. (At least, not without mutating the date formatter between operations.)
Use one formatter to convert the original string to a date. That formatter should not include AM/PM, since your original string doesn't.
Use a second formatter to convert the date to a new string. That formatter should include AM/PM, if you desire one.

Here 's how you should parse and convert the date:
//the date string
NSString *datestr = #"2014-03-14T10:35:24.537";
//strip the date
NSArray *components = [datestr componentsSeparatedByString:#"T"];
NSString *time = components[1];
time = [time substringWithRange:NSMakeRange(0, 5)];
//parse string to a date
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"HH:mm"];
NSDate *timeFromString = [formatter dateFromString:time];
//Desired format
NSDateFormatter *timeformat = [[NSDateFormatter alloc] init];
[timeformat setDateFormat:#"hh:mm aa"];
NSString *finalString = [timeformat stringFromDate:timeFromString];
NSLog(#"final = %#",finalString);
OUTPUT:
final = 10:35 AM

Related

How to Get Date, Hour, Minute and Second in Objective-c from Timestamp "2017-04-30T14:30+00:00(GMT)"?

I'm new in iOS(Objective-c) coding and I'm stuck at timestamp.
I'm getting timestamp while JSON parsing ie.2017-04-30T14:30+00:00(GMT). How to get date, hour, minute and second from this timestamp?? I'm getting this format in GMT so, is it possible to convert it into "IST"? How?
Date Format Patterns
A date pattern is a string of characters, where specific strings of characters are replaced with date and time data from a calendar when formatting or used to generate data for a calendar when parsing. The following are the characters used in patterns to show the appropriate formats for a given locale. The following are examples:
- (NSString *)curentDateStringFromDate:(NSDate *)dateTimeInLine withFormat:(NSString *)dateFormat {
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:dateFormat];
NSString *convertedString = [formatter stringFromDate:dateTimeInLine];
return convertedString;
}
Use it like below:
NSString *dateString = [self curentDateStringFromDate:[NSDate date] withFormat:#"dd-MM-yyyy"];
NSString *timeString = [self curentDateStringFromDate:[NSDate date] withFormat:#"hh:mm:ss"];
NSString *hoursString = [self curentDateStringFromDate:[NSDate date] withFormat:#"h"];
In the Foundation framework, the class to use for this task (in either direction) is NSDateFormatter Refer here
The code below convert GMT to IST.
NSString *inDateStr = #"2000/01/02 03:04:05";
NSString *s = #"yyyy/MM/dd HH:mm:ss";
// about input date(GMT)
NSDateFormatter *inDateFormatter = [[NSDateFormatter alloc] init];
inDateFormatter.dateFormat = s;
inDateFormatter.timeZone = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
NSDate *inDate = [inDateFormatter dateFromString:inDateStr];
// about output date(IST)
NSDateFormatter *outDateFormatter = [[NSDateFormatter alloc] init];
outDateFormatter.timeZone = [NSTimeZone timeZoneWithAbbreviation:#"IST"];
outDateFormatter.dateFormat = s;
NSString *outDateStr = [outDateFormatter stringFromDate:inDate];
// final output
NSLog(#"[in]%# -> [out]%#", inDateStr, outDateStr);

NSDateFormatterLongStyle string to NSDate

I have a UIDataPicker in my viewController with default location, when my user finishes selecting the date I run this code:
NSString *dateString = [NSDateFormatter localizedStringFromDate:[self.dataPicker date]
dateStyle:NSDateFormatterLongStyle
timeStyle:NSDateFormatterNoStyle];
With that code I can storage the date in the following format:
May 31, 2016
Later in my code I need to convert this string into a real date format, for this I use the code below:
-(NSDate*)convertStringToDate:(NSString*)date{
NSString *dateString = date;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US"];
[dateFormatter setDateFormat:#"MM/dd/yyyy"];
return [dateFormatter dateFromString:dateString];
}
But this code return a null value. As the datepicker is set by default, my system can receive any date format, but in the end I want it to be converted to the format en_us.
How I can solve this problem?
Don't store the date as a string; store it as an offset, in seconds, from some reference date.
i.e:
uint64_t offset = (uint64_t)[[self.dataPicker date] timeIntervalSinceReferenceDate];
// store this 64-bit unsigned integer.
This takes less space and is quicker to convert to/from an NSDate object.
You can leave the offset as an NSTimeInterval (64-bit floating point double) if you prefer, but as you aren't storing date & time, uint64_t should do...
Use this code,
-(NSDate*)convertStringToDate:(NSString*)date{
NSString *dateString = date;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US"];
[dateFormatter setDateFormat:#"MMM d, yyyy"];
return [dateFormatter dateFromString:dateString];
}
hope its helpful
The formatting string depends on the locale you are using. From the localizedStringFromDate documentation:
Returns string representation of a given date formatted for the
current locale using the specified date and time styles.
This method uses a date formatter configured with the current default
settings. The returned string is the same as if you configured and
used a date formatter as shown in the following example:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.formatterBehavior = NSDateFormatterBehavior10_4;
formatter.dateStyle = dateStyle; formatter.timeStyle = timeStyle;
NSString *result = [formatter stringForObjectValue:date];
Means, you should do the next:
-(NSDate*)convertStringToDate:(NSString*)dateString {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.formatterBehavior = NSDateFormatterBehavior10_4;
dateFormatter.dateStyle = NSDateFormatterLongStyle;
dateFormatter.timeStyle = NSDateFormatterNoStyle;
return [dateFormatter dateFromString:dateString];
}

How to convert arbitrary time to NSDate to format it using NSDateFormatter?

Let's say I have an NSString containing 15:00 PM. I am trying to use NSDateFormatter to format it to display
3:00 PM
However, when I try to print out the formatted string, it's null.
NSString *string = #"15:00 PM";
NSDateFormatter *formatter = [ [NSDateFormatter alloc] init];
[formatter setDateFormat:#"hh:mm a"];
NSDate *time = [formatter dateFromString:string];
string = [formatter stringFromDate:time];
NSLog(#"%#", string);
How should the string be assigned to properly format it?
Use following code,
NSString *string = #"15:00";
NSDateFormatter *inputDateFormatter = [[NSDateFormatter alloc] init];
[inputDateFormatter setDateFormat:#"HH:mm"];
NSDate *date = [inputDateFormatter dateFromString:string];
NSDateFormatter *outputDateFormatter = [[NSDateFormatter alloc] init];
[outputDateFormatter setDateFormat:#"hh:mm a"];
NSString *outputString = [outputDateFormatter stringFromDate:date];
I am in accord with Mikhail comment, you should write the pattern in this way:
[formatter setDateFormat:#"HH:mm a"];
The format string uses the format patterns from the Unicode Technical Standard # 35 (reference), for more information about date and time format in iOS, you can have a look here
Below is the screenshot which says that 24 hours format does not contain am/pm part:-
Use valid date format and then use the below code for setting to 12 hours time format:-
NSDate *time = [formatter dateFromString:string];
[formatter setDateFormat:#"hh:mm"];

NSString to NSDate with format conversation issue

I've already searched StackOverflow.com for an answer to this question, still without any success. Already looked here:
iOS - Converting time and date to user time zone
Date Format - time zone
Get current iPhone device timezone date and time from UTC-5 timezone date and time iPhone app?
IOS how to set date format
Nothing of those worked.
So I have this NSString date format: 2014-05-07T10:28:52.000Z trying to convert it to NSDate, this is what I'm using:
+ (NSDate*)stringToDate:(NSString*)string format:(NSString*)format
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:format];
return [dateFormatter dateFromString:string];
}
This is how I'm using it:
NSDate *date = [AppUtils stringToDate:youtube.postDate format:#"yyyy-MM-dd'T'HH:mm:ssZZZ"];
I've also tried those formats:
yyyy-MM-dd'T'HH:mm:ss.ZZZ
yyyy-MM-dd'T'HH:mm.ss.ZZZ
yyyy-MM-dd'T'HH:mm.ssZZZ
How could I convert it to NSDate successfully?
Thanks in advance!
If the date format is fixed, what I do is below.
Replace T by space
Replace Z by blank
And then do formatting...
NSString *dateReceivedInString = #"2014-05-07T10:28:52.000Z";
dateReceivedInString = [dateReceivedInString stringByReplacingOccurrencesOfString:#"T" withString:#" "];
dateReceivedInString = [dateReceivedInString stringByReplacingOccurrencesOfString:#"Z" withString:#""];
Now do the formatting using
yyyy-MM-dd HH:mm:ss
Edit 1
If you want to work with your case, use below
yyyy-MM-dd'T'HH:mm:ss.SSSZ
Edit 2
I tried this and it is working.
NSString *dateReceivedInString = #"2014-05-07T10:28:52.000Z";
dateReceivedInString = [dateReceivedInString stringByReplacingOccurrencesOfString:#"T" withString:#" "];
dateReceivedInString = [dateReceivedInString stringByReplacingOccurrencesOfString:#"Z" withString:#""];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss.SSS"];
NSLog(#"ddddd====%#", [dateFormatter dateFromString:dateReceivedInString]);
Edit 3
To make working with your case use below.
NSString *dateReceivedInString = #"2014-05-07T10:28:52.000Z";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSSZ"];
NSLog(#"ddddd====%#", [dateFormatter dateFromString:dateReceivedInString]);
Here are 2 methods that I use to convert RFC3339 Date string to NSDate,
And also Method for converting NSDate to RFC3339 Date string
Method for converting RFC3339 Date string to NSDate
+ (NSDate *)dateForRFC3339DateTimeString:(NSString *)rfc3339DateTimeString
{
NSDateFormatter *rfc3339DateFormatter = [[NSDateFormatter alloc] init];
[rfc3339DateFormatter setDateFormat:#"yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'SSS'Z'"];
[rfc3339DateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
// Convert the RFC 3339 date time string to an NSDate.
return [rfc3339DateFormatter dateFromString:rfc3339DateTimeString];}
Method for converting NSDate to RFC3339 Date string
+ (NSString *)RFC3339DateTimeFromDate:(NSDate *)aDate
{
NSDateFormatter *rfc3339DateFormatter = [[NSDateFormatter alloc] init];
[rfc3339DateFormatter setDateFormat:#"yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'SSS'Z'"];
[rfc3339DateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
return [rfc3339DateFormatter stringFromDate:aDate];
}

Converting NSString to Date

I have two strings: date1 = 3-3-2011;
and i want to convert in to 3-March-2011 and display it in label.
NSString *myString = 3-3-2011;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:#"d-MM-YYYY"];
NSDate *yourDate = [dateFormatter dateFromString:myString];
//now format this date to whatever you need…
[dateFormatter setDateFormat:#"d-MMM-YYYY"];
NSString *resultString = [dateFormatter stringFromDate:yourDate];
[dateFormatter release];
but yourdate = 2010-12-25 18:30:00 +0000
resultstring = 26-Dec-2010
i want 3-March-2010
please help!
Thank you.
You can use NSDateFormatter.
Convert your current string to NSDate object, so that you can
convert it into any format you want.
NSString *myString = #"3-3-2011";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:#"d-M-yyy"];
NSDate *yourDate = [dateFormatter dateFromString:myString];
Now you can convert this NSDate to any format you want.
[dateFormatter setDateFormat:#"d-MMMM-yyyy"];
NSString *resultString = [dateFormatter stringFromDate:yourDate];
[dateFormatter release];
Apple's documentation on NSDateFormatter is here.
Take a look at NSDateFormatter. In particular, take a look at the dateFromString: and stringFromDate: methods. You'll need to convert your original string to an NSDate, then convert that NSDate to another string.
One tip for using NSDateFormatter: be sure always to set a locale. If you don't manually set a locale, it has a bug regarding 12/24 hour clock settings. The example code on the page I linked to shows how to set a locale.
Use d-M-Y (or M-d-Y, depending on which is the month):
NSString *myString = 3-3-2011;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:#"d-M-Y"];
NSDate *yourDate = [dateFormatter dateFromString:myString];
And see the Unicode standard referred to in the Apple docs.

Resources