I need a NSString with the format "yyyy-mm-dd - 11:00:00"
Year, month, and date should be from the current date, while hour, minute, and second should be 11,00,00. I could not figure how to do that.
I think you mean yyyy-MM-dd - HH:mm:ss (because mm is minutes and MM is month). But you just need
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = #"yyyy-MM-dd - HH:mm:ss";
NSString *string = [formatter stringFromDate:[NSDate date]];
Or if you want it to say 11:00:00, either pass it that NSDate, or you could:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = #"yyyy-MM-dd' - 11:00:00'";
NSString *string = [formatter stringFromDate:[NSDate date]];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = #"yyyy-MM-dd";
NSString *string = [NSString stringWithFormat:#"%# - 11:00:00", [formatter stringFromDate:[NSDate date]]];
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 an NSString like this: #"3/15/2012 9:15 PM" and I would like to convert it to NSDate, I have done like this:
NSString *str =#"3/15/2012 9:15 PM";
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:#"mm/dd/yyyy HH:mm"];
NSDate *date = [formatter dateFromString:];
NSLog(#"%#", date); // date = null
Can you help me please, thanks.
Use the following solution
NSString *str = #"3/15/2012 9:15 PM";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = #"MM/dd/yyyy HH:mm a";
NSDate *date = [formatter dateFromString:str];
NSLog(#"%#", date);
Edit:
Sorry, the format should be as follows:
formatter.dateFormat = #"MM/dd/yyyy hh:mm a";
And the time shown will be GMT time. So if you add/subtract the timezone, it would be 9:15 PM.
Edit: #2
Use as below. You would get exact time too.
NSString *str = #"3/15/2012 9:15 PM";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = #"MM/dd/yyyy hh:mm a";
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
formatter.timeZone = gmt;
NSDate *date = [formatter dateFromString:str];
NSLog(#"%#",date);
Your formatter is wrong. According to the NSDateFormatter documentation it should be "MM/dd/yyyy h:mm a". You also probably want to set the locale as stated in the Date Formatting Guideline:
If you're working with fixed-format dates, you should first set the
locale of the date formatter to something appropriate for your fixed
format. In most cases the best locale to choose is en_US_POSIX, a
locale that's specifically designed to yield US English results
regardless of both user and system preferences.
Try this:
NSString *str = #"3/15/2012 9:15 AM";
NSLocale *enUSPOSIXLocale = [[[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"] autorelease];
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setLocale:enUSPOSIXLocale];
[formatter setDateFormat:#"MM/dd/yyyy h:mm a"];
NSDate *date = [formatter dateFromString:str];
NSLog(#"%#",date);
Outputs:
2012-03-19 12:37:27.531 Untitled[6554:707] 2012-03-15 08:15:00 +0000
Hmmm - you've gone wrong for a couple of reasons
the NSDateFormatter is strict and you have not been.
you didn't supply str to dateFromString:
NSString* str = #"3/15/2012 9:15 PM";
NSDateFormatter* formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:#"MM/dd/yyyy HH:mm a"];
NSDate* date = [formatter dateFromString:str];
NSLog(#"%#",date);
NSString *str =#"3/15/2012 9:15 PM";
NSDateFormatter *dateformatter = [[NSDateFormatter alloc]init];
[dateformatter setDateFormat:#"MM/dd/yyyy"];
NSArray *firstSplit = [str componentsSeparatedByString:#" "];
NSDate *dateSelected = [dateformatter dateFromString:[firstSplit objectAtIndex:0]];
[dateformatter setDateFormat:#"mm:ss"];
NSDate *dateSelected2 = [dateformatter dateFromString:[firstSplit objectAtIndex:1]];
NSLog(#"%#",[dateformatter stringFromDate:dateSelected2]);
NSLog(#"%#",[dateformatter stringFromDate:dateSelected]);
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);
I want to see what is getting stored in an NSDate, so I am using NSLog, but it's showing (null), whereas if I print the string stf2, it's showing the proper value.
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"YYYY-MM-dd"];
NSDateFormatter *formatter1 = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"MMM dd, yyyy"];
NSString *stf2 = [[pact.date componentsSeparatedByString:#" "] objectAtIndex:0];
NSLog(#"date %#",stf2);
NSDate *date_ = [formatter dateFromString:stf2];
pact.date = [formatter1 stringFromDate:date_];
NSLog(#"date %#",[NSDate date_]);
There are two specific problems in the code you've presented in the question.
Format Reset
First you do,
[formatter setDateFormat:#"YYYY-MM-dd"];
and then you initialize the second formatter followed by resetting the first formatter's format,
NSDateFormatter *formatter1 = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"MMM dd, yyyy"];
To emphasize
[formatter setDateFormat:#"MMM dd, yyyy"];
This should've been formatter1 but is formatter.
Date Format
If you look at the format you've use YYYY-MM-dd, it looks fine. But apparent YYYY have a different purpose and can be different from our usual calendar year. You should use the lowercase y instead.
[formatter setDateFormat:#"yyyy-MM-dd"];
And I don't think you meant this but
NSLog(#"date %#",[NSDate date_]);
should be
NSLog(#"date %#", date_);
you need to correct the dateformatter by setting proper date formatter. first do this
[formatter setDateFormat:#"yyyy-dd-MM"];
//it should be in the way as your string is. like if your string is 2011-Jun- 27 then fromatter should be
[formatter setDateFormat:#"yyyy-MM-dd"];
set the formatter as per your string's date format. then get the date back from this line
NSDate *date_ = [formatter dateFromString:stf2];
Assuming "stf2" is your string, then perhaps your object formatter is nil.
Below functions will be helpful to you.
"getDateTimeFromString" will take date and time as argument and it will return NSDate object.\
-(NSDate *)getDateTimeFromString :(NSString *)tempDate :(NSString *)tempTime{
NSString *dateValue = [tempDate stringByAppendingFormat:#" %#",tempTime];
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm"];
NSDate *date = [[NSDate alloc] init];
date = [dateFormatter dateFromString:dateValue];
NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
NSTimeZone* destinationTimeZone = [NSTimeZone systemTimeZone];
NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:date];
NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:date];
NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;
NSDate* destinationDate = [[[NSDate alloc] initWithTimeInterval:interval sinceDate:date] autorelease];
return date;
}
"getDateStringFromDate" will take NSDate as argument and it will return NSString.
So, you can NSLog that value.
-(NSString *)getDateStringFromDate :(NSDate *)dateValue{
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd"];
NSDateFormatter *timeFormat = [[NSDateFormatter alloc] init];
[timeFormat setTimeStyle:NSDateFormatterShortStyle];
[timeFormat setDateFormat:#"HH:mm a"];
NSString *theDate = [dateFormat stringFromDate:dateValue];
/*NSLog(#"\n"
"theDate: |%#| \n"
"theTime: |%#| \n"
, theDate, theTime);*/
return theDate;
}
Hope you will get the answer.