Date with Month name Objective-C - objective-c

I have an NSDate object with value for example: "2011-08-26 14:14:51", I want to display "8 August 2011" how to implement it? Thanks.

NSDate *date = [NSDate date]; //I'm using this just to show the this is how you convert a date
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateStyle:NSDateFormatterLongStyle]; // day, Full month and year
[df setTimeStyle:NSDateFormatterNoStyle]; // nothing
NSString *dateString = [df stringFromDate:date]; [df release];

NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease];
[df setDateFormat:#"dd LLLL yyyy"];
NSString *formattedDate = [df stringFromDate:[NSDate date]];

NSString *dateStr = #"2011-08-26 14:14:51";
//if you have date then,
NSString *dateStr = [NSString stringWithFormat:#"%#",yourDate];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"YYYY-MM-dd HH:mm:ss"];
NSDate *date = [dateFormat dateFromString:dateStr];
[dateFormat setDateFormat:#"MMMM dd, YYYY"];
NSString* temp = [dateFormat stringFromDate:date];
[dateFormat release];
NSLog(#"%#",temp);

Related

we get string form web service that string how to convert NsString to NSDate in objective c?

i have tried like this
cell.msgid.text=[[tableArray objectAtIndex:indexPath.row]objectForKey:#"MobileMessageMasterId"];
NSString *datefromweb= [[tableArray objectAtIndex:indexPath.row] objectForKey:#"MessageDateTime"];
NSDateFormatter *dateformatter = [[NSDateFormatter alloc] init];
[dateformatter setDateFormat:#"dd/MM/yyyy hh:mm:ss"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateformatter dateFromString:datefromweb];
[dateformatter setDateFormat:#"MMM yyyy"];
NSString *month1 = [dateformatter stringFromDate:dateFromString];
[dateformatter setDateFormat:#"hh:mm a"];
NSString *time= [dateformatter stringFromDate:dateFromString];
[dateformatter setDateFormat:#"dd"];
NSString *day1= [dateformatter stringFromDate:dateFromString];
we get string in datefromweb(NSString) variable but not get in
datefromstring(NSDate) variable and i am separate out date,time,month,year.
my response is
{ MessageDateTime = "19/01/2016 14:16:03";}
Try like this:
NSString *yourString = #""; // use your string here where you will get from JSON
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setTimeZone:[NSTimeZone localTimeZone]]; // Set timezone whatever you want
[formatter setDateFormat:#"dd/MM/yyyy HH:mm:ss"];
NSDate *convertedDate = [formatter dateFromString:yourString];
NSLog(#"%#",convertedDate);
//Get Date string from web
NSString *datefromweb= #"19/01/2016 14:16:03";
//Set Date Formatter
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"dd/MM/yyyy HH:mm:ss"];
//Convert string into Date
NSDate *selectedDate = [df dateFromString: datefromweb];
//For Setting new format to NSDate need to create new NSDateFormatter.
NSDateFormatter *df1 = [[NSDateFormatter alloc] init];
[df1 setDateFormat:#"dd"];
NSString* myDayString = [df1 stringFromDate:selectedDate];
[df1 setDateFormat:#"MMM"];
NSString* myMonthString = [df1 stringFromDate:selectedDate];
[df1 setDateFormat:#"MM"];
NSString* myMonthString1 = [df1 stringFromDate:selectedDate];
[df1 setDateFormat:#"yyyy"];
NSString* myYearString = [df1 stringFromDate:selectedDate];
[df1 setDateFormat:#"hh:mm a"];
NSString* myTimeString = [df1 stringFromDate:selectedDate];
Output :
Date 19
Month Name Jan - 01
Year 2016
Time 02:16 PM
Use NSDateComponents is better approach
NSDateFormatter *formatter = [NSDateFormatter new];
[formatter setDateFormat:#"dd/mm/yyyy HH:mm:ss"];
NSDate *date = [formatter dateFromString:#"19/01/2016 14:16:03"];
NSCalendar *gregorian = [NSCalendar currentCalendar];
NSDateComponents *comonents =
[gregorian components:(NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond
) fromDate:date];
NSInteger day = [comonents day];
NSInteger month = [comonents month];
NSInteger year = [comonents year];
NSInteger hour = [comonents hour];
NSInteger minutes = [comonents minute];
NSInteger seconds = [comonents second];

NsDateFormatter format the date

How can i this format this date ?
2/24/2017 11:13:43 AM
i tried this code but not worked
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MM/dd/yyyy HH:mm:ss"];
NSString *stringDate = [dateFormatter stringFromDate:createdDate];
You need to change the dateformate. You need to 'a' into formater.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MM/dd/yyyy hh:mm:ss aa"];
NSString *stringDate = [dateFormatter stringFromDate:[NSDate date]];
Output:
Printing description of stringDate:
02/25/2017 09:56:42 AM
You can see more about dateFormate.
Cheers

NSDateformatter incompatible format

Tried almost everything and get null,
the format I pass as parameter 'NSString' : WED 13-11-2013
-(NSDate*)stringToDate:(NSString*)dateString{
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
// [dateFormat setDateFormat:#"EE, d-LLLL-yyyy HH:mm:ss Z"];
// [dateFormat setDateFormat:#"EE, dd-MM-yyyy"];
[dateFormat setDateFormat:#"EEE, dd-MM-yyyy"];
// NSString *str = [dateString stringByAppendingString:#" 09:00:00 +0000"];
// NSDate *date = [dateFormat dateFromString:dateString];
NSDate *date = [dateFormat dateFromString:dateString];
return date;
}
Hi try that it works for me:
-(NSDate*)stringToDate:(NSString*)dateString{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"EEE dd-MM-yyyy ";
[dateFormatter setLocale:[NSLocale currentLocale]];
NSDate *date = [dateFormatter dateFromString:dateString];
return date
}

how to convert NSString to NSDate

i tried to convert this NSString date to NSDate
NSString *update_time= #"2012-03-09 14:54:30.0";
// convert to date
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"YYYY-MM-dd HH:mm:ss'.0'"];
[dateFormat setTimeZone:[NSTimeZone timeZoneWithName:#"Australia/Melbourne"]];
NSDate *dte = [dateFormat dateFromString:update_time ];
NSLog(#"Date: %#", dte);
but i'm getting Date: (null)
"GMT+4" is not a valid timzone name. Use timeZoneForSecondsFromGMT: instead:
//...
NSTimeZone *timeZone = [NSTimeZone timeZoneForSecondsFromGMT:4 * (60 * 60)];
[dateFormat setTimeZone:timeZone];
//...
If you want to use timeZoneWithName:, you can get a list of valid timezone names using [NSTimeZone knownTimeZoneNames]. Those all have the form "Europe/Berlin" etc.
NSString *update_time= #"2012-03-09 14:54:30.0";
Missing the pointer
You're missing a pointer.
NSString * update_time= #"2012-03-09 14:54:30.0";
// convert to date
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"YYYY-MM-dd HH:mm:ss'.0'"];
[dateFormat setTimeZone:[NSTimeZone timeZoneWithName:#"GMT+4"]];
NSDate *dte = [dateFormat dateFromString:update_time ];
NSLog(#"Date: %#", dte);

Retrieve Year and Month from string of date

I want to retrieve year and then month from this kind of date: 2011-12-23 10:45:01 with no luck.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"GMT"]];
[dateFormatter setDateFormat:#"yyyy"];
NSLog(#"Date = %#",[dateFormatter dateFromString:#"2011-12-23 10:45:01"]);
[dateFormatter release];
Date = (null), i can't understand why.
You have to do it in two steps, first match the whole date, then output the bits you want:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"GMT"]];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSDate *date = [dateFormatter dateFromString:#"2011-12-23 10:45:01"];
//now you have the date, you can output the bits you want
[dateFormatter setDateFormat:#"yyyy"];
NSString *year = [dateFormatter stringFromDate:date];
[dateFormatter setDateFormat:#"MM"];
NSString *month = [dateFormatter stringFromDate:date];
[dateFormatter release];
This was already answered in Formatting NSDate into particular styles for both year, month, day, and hour, minute, seconds.
Specifically:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd"];
NSDateFormatter *timeFormat = [[NSDateFormatter alloc] init];
[timeFormat setDateFormat:#"HH:mm:ss"];
NSDate *now = [[NSDate alloc] init];
NSString *theDate = [dateFormat stringFromDate:now];
NSString *theTime = [timeFormat stringFromDate:now];
NSLog(#"\n"
"theDate: |%#| \n"
"theTime: |%#| \n"
, theDate, theTime);
[dateFormat release];
[timeFormat release];
[now release];