iOS Date format 28 Mar 2016 / 16:54pm - ios

I want to display the date in the format "28 Mar 2016 / 16:54pm" from the string "2017-05-05T08:27:31.337Z". How can I do this? Is there any buildin method for iOS? Thanks.

use NSDateFormatter for your concept
for e.g
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyyy-MM-dd'T'HH:mm:ss.SSSZ"];
NSDate *msgDate = [dateFormatter dateFromString:#"2017-05-05T08:27:31.337Z"];
[dateFormatter setDateFormat:#"dd MMM yyyy / HH:mma"];
NSString *final = [dateFormatter stringFromDate:msgDate];
update
add setAMSymbol and setPMSymbol in your dateFormatter.
[dateFormatter setAMSymbol:#"am"];
[dateFormatter setPMSymbol:#"pm"];
Full Answer
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyyy-MM-dd'T'HH:mm:ss.SSSZ"];
NSDate *msgDate = [dateFormatter dateFromString:#"2017-05-05T08:27:31.337Z"];
[dateFormatter setDateFormat:#"dd MMM yyyy / HH:mma"];
[dateFormatter setAMSymbol:#"am"];
[dateFormatter setPMSymbol:#"pm"];
NSString *final = [dateFormatter stringFromDate:msgDate];

See Instruction or introduction with every line
NSString *str = #"your date here..."; /// here this is your date with format yyyyy-MM-dd'T'HH:mm:ss.SSSZ
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; // here we create NSDateFormatter object for change the Format of date..
[dateFormatter setDateFormat:#"yyyyy-MM-dd'T'HH:mm:ss.SSSZ"]; //// here set format of date which is in your output date (means above str with format)
NSDate *date = [dateFormatter dateFromString: str]; // here you can fetch date from string with define format
dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd MMM yyyy / HH:mma"];// here set format which you want...
NSString *convertedString = [dateFormatter stringFromDate:date]; //here convert date in NSString
NSLog(#"Converted String : %#",convertedString);

Related

Unable to get date from string?

I Get DateTime string from server "8/27/2016 6:56:23 AM"
And I want to fetch only date from this string.
NSString *StreamStartDateTime=#"8/27/2016 6:56:23 AM";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyyMMdd"];
NSDate *date1=[dateFormatter dateFromString:StreamStartDateTime];
But the problem is I am getting null in date1.
please help me to find out where I am doing wrong.
You are using the wrong dateFormat. Please replace your code with following:
NSString *StreamStartDateTime=#"8/27/2016 6:56:23 AM";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MM/dd/yyyy hh:mm:ss a"];
NSDate *date1=[dateFormatter dateFromString:StreamStartDateTime];
do like
NSString *StreamStartDateTime=#"8/27/2016 6:56:23 AM";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MM/dd/yyyy h:mm:ss a"]; or use hh:mm:ss a
NSDate *date1=[dateFormatter dateFromString:StreamStartDateTime];
[dateFormatter setDateFormat:#"yyyyMMdd"];
NSString *ssecond=[dateFormatter stringFromDate:date1];
NSDate *finalDate=[dateFormatter dateFromString:ssecond];
final output as
Your date format must be match with your date string.
So If your date string is 8/27/2016 6:56:23 AM then your format must be MM/dd/yyyy hh:mm:ss a. Then with this format get date from string first like,
NSDate *date=[df dateFromString:StreamStartDateTime];
Now, change date formatter in whatever format you want your date say,
[df setDateFormat:#"yyyyMMdd"];
then
NSString *resultDateStr = [df stringFromDate:date];
Can try like this
NSString *StreamStartDateTime=#"8/27/2016 6:56:23 AM";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"MM/dd/yyyy h:mm:ss a"];
NSDate *streamStartDate = [formatter dateFromString:StreamStartDateTime];
[formatter setDateFormat:#"yyyyMMdd"];
NSString *strDate = [formatter stringFromDate:streamStartDate];
NSDate *compDate = [formatter dateFromString:strDate]; // Get date in UTC format
Output:
Printing description of strDate:
20160827
Printing description of compDate:
2016-08-26 18:30:00 +0000
NSString *LiveStreamStartDateTime=#"8/27/2016 6:56:23 AM";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MM/dd/yyyy h:mm:ss a"];
NSDate *date1=[dateFormatter dateFromString:LiveStreamStartDateTime];
[dateFormatter setDateFormat:#"MM/dd/yyyy"];
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"]];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"UTC"]];
NSString *fisrtdate=[dateFormatter stringFromDate:date1];
NSDate *date11 = [dateFormatter dateFromString:fisrtdate];

NSDateFormatter for day and month name

I am trying to get date in the format Monday, March 09, 2015. But following code is not returning required date format. I think I am using wrong Formatter. Here is the code:
NSString *dateString = #"09-03-2015";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd MMMM yyyy"];
NSDate *date = [[NSDate alloc] init];
date = [dateFormatter dateFromString:dateString];
NSLog(#"%#",[dateFormatter stringFromDate:date]);
try this...
//Getting date from string
NSString *dateString = #"09-03-2015";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
NSDate *date = [[NSDate alloc] init];
date = [dateFormatter dateFromString:dateString];
// converting into our required date format
[dateFormatter setDateFormat:#"EEEE, MMMM dd, yyyy"];
NSString *reqDateString = [dateFormatter stringFromDate:date];
NSLog(#"date is %#", reqDateString);
LOG:2015-03-09 12:40:33.456 TestCode [1377:38775] date is Monday, March 09, 2015
Firstly you must convert your dateString to NSDatevalue
NSString *dateString = #"09-03-2015";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
NSDate *date = [[NSDate alloc] init];
date = [dateFormatter dateFromString:dateString];
NSLog(#"%#",[dateFormatter stringFromDate:date]);
after that you could use this format: #"EEEE,MMMM dd, yyyy" to convert that dateValue to dateString like Monday, March 09, 2015
Hope this help
Helpful link for you
Try to set dateFormat property to #"dd'-'MM'-'yyyy" . Always check the unicode the date symbol table. "MMMM" means that the date month should be a full month name.
Try this one..
NSString * yourJSONString = #"09-03-2015";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];;
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
[dateFormatter setLocale:[NSLocale currentLocale]];
NSDate *dateFromString = [dateFormatter dateFromString:yourJSONString];
[dateFormatter setDateFormat:#"EEEE,LLLL dd, yyyy"];
NSString *output = [dateFormatter stringFromDate:dateFromString];
NSLog(#"%#", output);
Try this:-
NSDate *date = [NSDate date];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
NSString * format = [NSDateFormatter dateFormatFromTemplate:#"EEEEMMMMdyyyy" options:0 locale:[NSLocale currentLocale]];
[formatter setDateFormat:format];
NSString *dateFormatted = [fullDateFormatterTime stringFromDate: date];
NSLog(#"Formatted date: %#", dateFormatted);

how to convert nsdate to another format using nsdateformatter

I have date string in this format Feb 23, 2014 coming from web services, I want to convert it to this format 02-23-2014, I am trying a lot but all the time nsstring return me null value
NSDateFormatter *DateFormatter=[[NSDateFormatter alloc] init];
[DateFormatter setDateStyle:NSDateFormatterMediumStyle];
[DateFormatter setDateFormat:#"MM-dd-yyyy"];
// date formatter two
NSDate *dateVal = (NSDate *)bookDetail.classdate;
NSLog(#"date %#",dateVal);
NSString *dateTwo = [DateFormatter stringFromDate:dateVal];
NSLog(#"new date string %#",dateTwo);
NSString *dateString = #"Feb 23, 2014";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MMM dd, yyyy"];
NSDate *date = [dateFormatter dateFromString:dateString];
[dateFormatter setDateFormat:#"MM-dd-yyyy"];
NSString *newDateString = [dateFormatter stringFromDate:date];

Convert NSString to NSDate and back to different format NSString

I need to take the NSString Dec 4, 2012, 12:33 PM and convert it to separate out the month, day, and year, so that I can have 3 different strings of 12, 04, and 2012.
I figure that I should convert the NSString to NSDate and then reformat the date to change out NSString, but am running into issues.
I have:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MMM dd, yyyy, hh:mm p"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:substring];
NSLog(#"Date%#", dateFromString);
[dateFormatter release];
However, the date keeps coming back null.
The problem is with your locale I think.
You should try to print how your dateFormatter formats current date [NSDate new].
It works for me:
NSString* substring = #"Dec 12 2012 12:08 PM";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US"];
[dateFormatter setDateFormat:#"MMM d yyyy h:mm a"]; // not 'p' but 'a'
NSDate *dateFromString = [dateFormatter dateFromString:substring];
Convert
[dateFormatter setDateFormat:#"MMM dd, yyyy, hh:mm p"];
to, Because MMM dd, yyyy, hh:mm p not a valid date formate
[dateFormatter setDateFormat:#"MMM dd, yyyy, hh:mm a"];
And
NSDate *dateFromString = [dateFormatter dateFromString:substring];
To get string again
NSLog(#"%#",[dateFormatter stringFromDate:dateFromString]);
for me NSLog is Dec 04, 2012, 12:33 PM
#import "NSString+Date.h"
#implementation NSString (Date)
+ (NSDate*)stringDateFromString:(NSString*)string;
{
NSString *dateString = #"01-02-2010";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
// this is imporant - we set our input date format to match our input string
// if format doesn't match you'll get nil from your string, so be careful
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
NSDate *dateFromString = [[NSDate alloc] init];
// voila!
dateFromString = [dateFormatter dateFromString:dateString];
return dateFromString;
}
+(NSString*)StringFromDate :(NSDate*)date;
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
NSString *stringDate = [dateFormatter stringFromDate:[NSDate date]];
NSLog(#"%#", stringDate);
return stringDate;
}
` #end

How to change date format from string to dd/mm/yy on iOS?

So i am parsing an xml file and display on screen the dates that i parse.
The dates are like : Thu , 31 May 2012 20:43:54 GMT.
How can i convert the date to this format : dd/mm/yy ?
i dont need the time.
Well it crashes.
I tried :
NSString *date = [[stories objectAtIndex: storyIndex] objectForKey: #"date"];
//changing dates format
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat: #"yyyy-MM-dd HH:mm:ss Z"];
NSDate *myDate = [dateFormatter dateFromString:date];
NSString *dateText = [[NSString alloc] initWithString:[dateFormatter stringFromDate:myDate]];
Any ideas?
You can use NSDateFormatter
For example:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat: #"yyyy-MM-dd HH:mm:ss Z"];
NSDate *myDate = [dateFormatter dateFromString:yourString];
See http://unicode.org/reports/tr35/tr35-6.html#Date_Format_Patterns for dateformat patterns
The idea is to use NSDateFormatter to get a date from your input time string, then use a new dateformatter to get whatever new time string you want from the date.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE, d MMM YYYY HH:mm:ss Z"];
NSDate *date = [dateFormatter dateFromString:theDateString];
[dateFormatter release];
NSDateFormatter *dateFormatter2 = [[NSDateFormatter alloc] init];
[dateFormatter2 setDateFormat:#"dd/mm/yyyy"];
NSString *newDateString = [dateFormatter2 stringFromDate:date];
[dateFormatter2 release];
Alternatively for the second part You can use NSDateComponents.
after:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE, d MMM YYYY HH:mm:ss Z"];
NSDate *date = [dateFormatter dateFromString:theDateString];
[dateFormatter release];
//Get NSComponents from date object..
[NSString stringWithFormat:#"%#/%#/%#",dateComponent.date,dateComponent.month,dateComponent.year];

Resources