i need help., I have this date in a UIlabel in this format 04/26/2013 . How can I re format it into 04.26.2013 ? I already have below codes:
UILabel *timelabel = [[UILabel alloc] initWithFrame:CGRectMake(xx, xx, xx, xx)];
[timelabel setText:dateToday];
dateToday value is 04/26/2013.
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"MM.dd.yyyy"];
NSString *stringFromDate = [formatter stringFromDate:[NSDate date]];
NSLog(#"today : %#", stringFromDate);
timelabel.text = [dateToday stringByReplacingOccurrencesOfString:#"/" withString:#"."];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MM.dd.yyyy"];
NSDate *date = [dateFormatter dateFromString:dateToday];
if(date != nil) {
NSString *formattedDateString = [dateFormatter stringFromDate:date];
[timelabel setText:formattedDateString];
} else {
[timelabel setText:#"Invalid Date"];
}
Added error checking. Always check if the input string is a valid date string or not.
Reference: Date Formatting Guide (though it is for Mac; iOS applies the same)
Related
This question already has answers here:
Date format for 2016-10-20T13:01:47.317
(4 answers)
Closed 5 years ago.
I'm trying to get date asNSString. I'm receiving date in the format 2017-05-05T04:42:44.954604Z
Below is the code in which I'm trying to apply NSDateFormatter
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSString *newdate = [utcDate stringByReplacingOccurrencesOfString:#"T" withString:#" "];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss.Z"];
msgDate = [dateFormatter dateFromString:newdate];
But msgDate is showing nil
use 'SS' specifier (with number of S's equal to number of digits you want to get - 6 in your case)
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSSSSSZ"];
instead of
NSString *newdate = [utcDate stringByReplacingOccurrencesOfString:#"T" withString:#" "];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss.Z"];
UPDATE
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyyy-MM-dd'T'HH:mm:ss.SSSSSSZ"];
NSDate *msgDate = [dateFormatter dateFromString:#"2017-05-05T04:42:44.954604Z"];
OUTPUT
#pragma mark- Change dateFormat method
+ (NSString*) changeDateFormat:(NSString*)date
{
NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSSZ"];
NSDate* fromDate = [formatter dateFromString:date];
[formatter setDateFormat:#"YYYY.MM.dd"];
NSString* formatedDate = [formatter stringFromDate:fromDate];
if (formatedDate == nil)
{
[formatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss"];
fromDate = [formatter dateFromString:date];
[formatter setDateFormat:#"YYYY.MM.dd"];
formatedDate = [formatter stringFromDate:fromDate];
}
if (formatedDate == nil)
{
[formatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZ"];
fromDate = [formatter dateFromString:date];
[formatter setDateFormat:#"YYYY.MM.dd"];
formatedDate = [formatter stringFromDate:fromDate];
}
return formatedDate;
}
Try this
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"yyyy-MM-dd'T'HH:mm:ss.SSSSSSZ";// chang
NSDate *date = [dateFormatter dateFromString:#"2017-04-13T05:57:44.886Z"];
NSString * msgDate = [dateFormatter stringFromDate:date];
// if get nil **date** try to change this time format
Hi I need to convert this string
"2013-12-05T08:58:55.9345456+02:00"
to NSDate
I am trying next format but without luck
"yyyy-MM-dd'T'HH:mm:SSSSSSSZZZZZ"
Code below.
Can you help me with right format
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
// "2013-12-05T08:58:55.9345456+02:00"
[dateFormat setDateFormat:#"yyyy-MM-dd'T'HH:mm:SSSSSSSZZZZZ"];
NSDate *date = [dateFormat dateFromString:jsonDateString];
Your format is close. You need:
yyyy-MM-dd'T'HH:mm:ss.SSSSSSSZZZZZ
And be sure to set the formatter's locale to en_US_POSIX. This should always be done when parsing fixed formatted, non-localized strings.
NSString * dateStr = #"2013-12-05T08:58:55.9345456+02:00";
NSArray *dateStrParts = [dateStr componentsSeparatedByString:#"T"];
NSString *datePart = [dateStrParts objectAtIndex:0];
NSString *timePart = [dateStrParts objectAtIndex:1];
NSString *t = [[timePart componentsSeparatedByString:#"."] objectAtIndex:0];
NSString *newDateStr = [NSString stringWithFormat:#"%# %#",datePart,t];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
[df setDateFormat:#"yyyy-MM-dd HH:mm:ss"]; // Change here for your formated output
NSDate *date = [df dateFromString:newDateStr];
NSLog(#"%#",date);
Output
2013-12-05 08:58:55 +0000
The problem lies in the timezone where you have +02:00.
The ZZZZ only parses to +0000, hence you are getting nil.
You can do in this way:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
NSString *jsonDateString = #"2013-12-05T08:58:55.9345456+02:00";//#"2013-12-05T08:58:55.9345456+02:00"
NSRange lastColon = [jsonDateString rangeOfString:#":" options:NSBackwardsSearch];
if(lastColon.location != NSNotFound) {
jsonDateString = [jsonDateString stringByReplacingCharactersInRange:lastColon withString: #""];
}
[dateFormat setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss'.'SZ"];
NSDate *date = [dateFormat dateFromString:jsonDateString];
NSLog(#"%#",date)
EDIT:
As per rmaddy's comment, you should use SSSSS instead of single S.
I have a UIDatePicker and I m getting the selected date from it in yyyy-MM-dd format.Now I want to convert it to MM/dd/yyyy format ..How can i do it ?
NSDateFormatter *df=[[[NSDateFormatter alloc]init] autorelease];
df.dateFormat = #"yyyy-MM-dd";
NSArray *temp=[[NSString stringWithFormat:#"%#",[df stringFromDate:DatePicker.date]] componentsSeparatedByString:#""];
[dateString2 release];
dateString2=nil;
dateString2= [[NSString alloc]initWithString:[temp objectAtIndex:0]];
NSLog(#"%#",dateString2);
Im getting 2012-10-30 but I want 10/30/2012.
See Instruction or introduction with every line
NSString *str = #"2012-10-30"; /// here this is your date with format yyyy-MM-dd
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease]; // here we create NSDateFormatter object for change the Format of date..
[dateFormatter setDateFormat:#"yyyy-MM-dd"]; //// 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] autorelease];
[dateFormatter setDateFormat:#"dd/MM/yyyy"];// here set format which you want...
NSString *convertedString = [dateFormatter stringFromDate:date]; //here convert date in NSString
NSLog(#"Converted String : %#",convertedString);
df.dateFormat = #"MM/dd/yyyy";
NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateFormat:#"MM/dd/yyyy"];
Just to see how it ends up:
NSDateFormatter* df = [[[NSDateFormatter alloc]init] autorelease];
df.dateFormat = #"MM/dd/yyyy";
NSString* dateString = [df stringFromDate:datePicker.date]; // Don't use leading caps on variables
NSLog(#"%#", dateString);
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"MM/dd/yyyy"];
The original accepted answer does a few things extra:
1) It allocates two NSDateFormatter instances which is not required. NSDateFormatter is expensive to create.
2) It is better to release date formatters explicitly when you are done with them rather than a deferred release using autorelease.
//get datepicker date (NSDate *)
NSDate *datePickerDate = [myDatePicker date];
//create a date formatter
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
//set its format as the required output format
[formatter setDateFormat:#"MM/dd/yyyy"];
//get the output date as string
NSString *outputDateString = [formatter stringFromDate:datePickerDate];
//release formatter
[formatter release];
- (void)showTime {
NSDate *now = [NSDate date];
NSDateFormatter *dateFormatter = [NSDateFormatter new];
[dateFormatter setDateFormat:#"HH:mm:ss"];
// you label
_time.text = [dateFormatter stringFromDate:now];
}
- (void)showDate {
NSDate *now = [NSDate date];
NSDateFormatter *dateFormatter = [NSDateFormatter new];
[dateFormatter setDateFormat:#"MM/dd/yyyy"];
// you label
_date.text = [dateFormatter stringFromDate:now];
}
- (void)viewDidLoad {
[super viewDidLoad];
self.showTime;
self.showDate;
}
Enjoy
NSString *str = #"2012-10-30"; /// here this is your date with format yyyy-MM-dd
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease]; // here we create NSDateFormatter object for change the Format of date..
[dateFormatter setDateFormat:#"yyyy-MM-dd"]; //// 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] autorelease];
[dateFormatter setDateFormat:#"dd/MM/yyyy"];// here set format which you want...
NSString *convertedString = [dateFormatter stringFromDate:date]; here convert date in NSString
NSLog(#"Converted String : %#",convertedString);
I use a code with options NSISO8601DateFormatWithDashSeparatorInDate:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSString * stringformat = [NSDateFormatter dateFormatFromTemplate:#"MMddy" options:NSISO8601DateFormatWithDashSeparatorInDate locale:[NSLocale systemLocale]];
[dateFormatter setDateFormat:stringformat];
NSDate *d = [NSDate date]; // or set your date
NSLog(#"date in format %#", [dateFormatter stringFromDate:d]); // date in format 2018-06-20
Good day, I'm trying to convert NSString that I'm getting from the server to NSDate object.
That is the code:
NSString *date = #"Sep 12 at 12:03 am";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MMM d 'at' H:m a"];
self.messageDateAndTime = [dateFormatter dateFromString:date];
NSLog(#"NSDate: %#", self.messageDateAndTime);
I'm getting date in the format that date string has. After I'm trying to create my own formatter for this date format. But anyway, I'm getting null! What I'm doing wrong?
I debug you above code its running fine and giving NSDate: 1970-09-11 18:33:00 +0000 this log. Check for you string first. For more you can use the category below.
// NSString+Date.h
#interface NSString (Date)
+ (NSDate*)stringDateFromString:(NSString*)string;
+ (NSString*)stringDateFromDate:(NSDate*)date;
#end
// NSString+Date.m
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormat setDateFormat:#"EEE, dd MMM yyyy HH:mm:ss ZZZ"];
NSDate *date = [dateFormatter dateFromString:stringDate ];
[dateFormatter release];
+ (NSDateFormatter*)stringDateFormatter
{
static NSDateFormatter* formatter = nil;
if (formatter == nil)
{
formatter = [NSDateFormatter alloc] init];
[formatter setDateFormat:#"EEE, dd MMM yyyy HH:mm:ss ZZZ"];
}
return formatter;
}
+ (NSDate*)stringDateFromString:(NSString*)string
{
return [[NSString stringDateFormatter] dateFromString:string];
}
+ (NSString*)stringDateFromDate:(NSDate*)date
{
return [[NSString stringDateFormatter] stringFromDate:date];
}
// Usage (#import "NSString+Date.h")
NSString* string = [NSString stringDateFromDate:[NSDate date]];
NSDate* date = [NSString stringDateFromString:string];
Please find the correct NSDateFormatter formatting string here.
I found the solution my self, it would return null until you set the locale yourself. This is the code:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US"];
[dateFormatter setLocale:usLocale];
[dateFormatter setDateFormat:#"MMM d 'at' H:m a"];
self.messageDateAndTime =[dateFormatter dateFromString:messageDate];
I have got a particular date from the string
NSString *str = #"2016-05-04 08:42:00 +0000";
And i want to convert this into 08:42 AM format.
Here is my code
NSString *str = #"2016-05-04 08:42:00 +0000";// put here item.TimeStart
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init] ;
[dateFormatter1 setDateFormat:#"EEE, dd MMM YYYY HH:mm:ss VVVV"];// here give the format which you get in TimeStart
NSDate *date1 = [dateFormatter1 dateFromString: str];
dateFormatter1 = [[NSDateFormatter alloc] init] ;
[dateFormatter1 setDateFormat:#"hh:mm a"];
NSString *convertedString = [dateFormatter stringFromDate:date1];
NSLog(#"Converted String : %#",convertedString);
But this is returning nil in Converted String. Why ?? Any idea??
Please add the following line to set the time zone time difference to 0 and you will get the actual time.
//Add this line to set the time zone time difference to 0
dateFormatter1.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
After adding this, Your final code will look line as follows:
NSString *str = #"2016-05-04 08:42:00 +0000";
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init] ;
[dateFormatter1 setDateFormat:#"YYYY-dd-MM HH:mm:ss VVVV"];
NSDate *date1 = [dateFormatter1 dateFromString: str];
dateFormatter1 = [[NSDateFormatter alloc] init];
//Add this line to set the time zone time difference to 0
dateFormatter1.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
[dateFormatter1 setDateFormat:#"hh:mm a"];
NSString *convertedString = [dateFormatter1 stringFromDate:date1];
NSLog(#"Converted String : %#",convertedString);
It will help you
-(NSDate *)calculateDate:(NSString *)getString
{
NSDateFormatter *Format = [[NSDateFormatter alloc] init];
[Format setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss"];
NSDate *aDate = [Format dateFromString:getString];
if( !aDate )
{
[Format setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss a"];
[Format setLocale:[NSLocale localeWithLocaleIdentifier:#"en_US_POSIX"]];
aDate = [Format dateFromString:getString];
}
return aDate;
}
When we have a string like 2001-02-15T13:25:00 and we want to format it to 12H format, we just need to define the local.