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
Related
I know that it sounds incessantly familiar, but most of the suggested solutions on SO have not worked for me for some strange reason.
I have a date string returned from an SQLite query as an NSString in this format:
2019-06-10 13:45:33
However, when any of the suggested date formatter solutions are applied, with or without timezone localisation, I keep getting such a result:
Mon Jun 10 13:45:33 2019
This is one of the routines I've tried, among many others:
NSString * dateString = #"2019-06-10 13:45:33";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
//[dateFormatter setDateFormat:#"YYYY-MM-dd HH:mm:ss"];
//[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss ZZZ"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateString];
// dateFromString > Mon Jun 10 13:45:33 2019
Could I be doing something wrong or is there some missing step in the conversion?
TIA.
I could guess that you wanted another output format, if it is the case then you could try code like this:
NSString * dateString = #"2019-06-10 13:45:33";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
//[dateFormatter setDateFormat:#"YYYY-MM-dd HH:mm:ss"];
//[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss ZZZ"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateString];
NSDateFormatter *printDateFormatter = [[NSDateFormatter alloc] init];
printDateFormatter.dateStyle = NSDateIntervalFormatterMediumStyle;
printDateFormatter.timeStyle = NSDateIntervalFormatterMediumStyle;
NSLog(#"%#", [printDateFormatter stringFromDate:dateFromString]);
The result will be:
10 Jun 2019 at 13:45:33
Use a different formatter to format the string from the date. For example:
NSDateFormatter * formatter=[[NSDateFormatter]alloc]init];
formatter.dateStyle = NSDateFormatterMediumStyle;
formatter.timeStyle = NSDateFormatterNoStyle;
NSString * formattedDateString = [formatter stringFromDate:date];
setDateFormat is for inputting date strings and getting NSDates.
dateStyle and timeStyle is for formatting dateStrings from NSDates.
All,
I want date format in dd/MMM/yyyy format below is the code i am using.
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateStyle:#"dd/MMM/yyyy"];
NSString *dateString = [formatter stringFromDate:[NSDate date]];
But in the dateString i am getting in Oct 21,2014 which is diffrent how can i get in "21/Oct/2014" format please let me know
The issue is that you're calling setDateStyle: instead of setDateFormat:
Use this code
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"dd/MMM/yyyy"];
NSString *dateString = [dateFormat stringFromDate:[NSDate date]];
NSLog(#"%#", dateString);
This code outputs "22/Oct/2014"
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];
can anyone please tell me why this code snippet returns nil in my log file:
NSString *dateString = [[NSString alloc] initWithString: #"2011-08-23 15:00"];
NSLocale *englishLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"en"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"YYYY-mm-dd HH:MM"];
[dateFormatter setLocale:englishLocale];
NSLog(#"%#",[dateFormatter dateFromString:dateString]);
Thanks a lot.
Check my variant:
NSString *dateString = [[NSString alloc] initWithString: #"2011-08-23 15:00"];
NSLocale *englishLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"en"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
//HH:MM -> HH:mm
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm"];
[dateFormatter setLocale:englishLocale];
NSLog(#"%#",[dateFormatter dateFromString:dateString]);
As you see I've changed date format to : #"yyyy-MM-dd HH:mm"
All formats can be found here: UNICODE LOCALE DATA MARKUP LANGUAGE (LDML)
you should be correct 'HH:MM' to 'HH:mm'
NSString *dateString = [[NSString alloc] initWithString: #"2011-08-23 15:00"];
NSLocale *englishLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"en"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
//HH:MM -> HH:mm
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm"];
[dateFormatter setLocale:englishLocale];
NSLog(#"%#",[dateFormatter dateFromString:dateString]);
http://www.picksourcecode.com/ps/ct/161110.php
For more dateFormats
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);