I want to use date format like this 2015-08-14 but unfortunately I am getting this format 16/11/15
Below is the code I have used to get desired output.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
[dateFormatter setTimeZone:gmt];
[dateFormatter setDateStyle:kCFDateFormatterShortStyle];
NSString *formattedDateString = [dateFormatter stringFromDate:selectedDate];
Can anyone please suggest how to fix this issue?
Just remove the line [dateFormatter setDateStyle:NSDateFormatterShortStyle];
-(NSString*)stringFromDateWithFormat:(NSString*)formatString{
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
dateFormatter.dateFormat = formatString;
return [dateFormatter stringFromDate:self];
}
NSDate *currDate = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:#"YYYY-MM-dd"];
NSString *dateString = [dateFormatter stringFromDate:currDate];
NSLog(#"date is %#",dateString);
NSDate *selectedDate = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
[dateFormatter setTimeZone:gmt];
NSString *formattedDateString = [dateFormatter stringFromDate:selectedDate];
NSLog(#"formattedDateString = %#", formattedDateString); // Prints 2015-10-15
Related
The date is always nil, but if I change the dateString to any other date like 03/01/1975 it works normally. Any idea?
NSString *dateString = #"05/01/1975";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSLocale *gbLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"us"];
[dateFormatter setLocale:gbLocale];
[dateFormatter setDateFormat:#"MM/dd/yyyy"];
NSDate *date = [dateFormatter dateFromString:dateString];
Try this
NSString *dateString = #"05/01/1975";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setTimeZone:[NSTimeZone timeZoneWithName:#"UTC"]];
[formatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"]];
[formatter setDateFormat:#"MM.dd.yyyy"];
NSDate *anotherDate = [formatter dateFromString:dateString];
NSLog(#"%#",anotherDate);
It will give output like this
1975-05-01 00:00:00 +0000
try this code
- (NSDate *)getPickerDateForString:(NSString *)dateString
{
NSDate * dt;
if (!dateFormatter)
{
dateFormatter = [[NSDateFormatter alloc] init];
}
[dateFormatter setDateFormat:#"dd/MM/yyyy"];
dt = [dateFormatter dateFromString:dateString];
return dt;
}
I try to convert NSString to NSDate with this code:
NSString *dateString=[[NSString alloc] init];
dateString = [[messageArray objectAtIndex:i] tim_dte];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"HH:mm,yyyy/MM/dd"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateString];
msg.date = dateFromString;
But msg.Date is nil even if dateString shows the correct date string. What am I missing?
change this format
[dateFormatter setDateFormat:#"HH:mm,yyyy/MM/dd"];
into and try
[dateFormatter setDateFormat:#"yyyy/MM/dd HH:mm"];
Update
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy/MM/dd HH:mm"];
// use simple
msg.date = [dateFormatter dateFromString:#"2016/05/06 12:36"];
NSLog(#"final date == %#",dateFromString);
//NSDate *dateFromString = [dateFormatter dateFromString:#"2016/05/06 12:36"];
// NSLog(#"final date == %#",dateFromString);
Try this code to convert NSString to NSDate
NSString *dateString = #"2016-05-06";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZ"];
NSDate *date = [dateFormatter dateFromString:#"2013-02-01T06:25:47Z"];
NSTimeZone *pdt = [NSTimeZone timeZoneWithAbbreviation:#"PDT"];
[dateFormatter setTimeZone:pdt];
[dateFormatter setDateFormat:#"HH:mm:ss zzz"];
[dateFormatter setDateFormat:#"K:mm a, z"];
NSString * updated String = [dateFormatter stringFromDate:date];
Use this Code,
NSString *dateString=[[NSString alloc] init];
dateString =[[messageArray objectAtIndex:i] tim_dte];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy/MM/dd HH:mm"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateString];
msg.date=dateFromString;
hope its helpful
i came across a problem in my iPhone application.
in my app i converte time string to date like that
NSString *time = #"01:30 PM";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"hh:mm a"];
NSDate *date = [dateFormat dateFromString:time];
and for some reason always date is nil after [dateFormat dateFromString:time].
can anyone tell me what i'm doing wrong?
thanks
I'm assuming your device language is other then English. In that case you need to set the date formatter Locale. Try this code.
NSString *time = #"01:30 PM";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"hh:mm a"];
[dateFormat setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"]];
try this...
NSString *time = #"01:30 pm";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setTimeStyle:NSDateFormatterShortStyle];
[dateFormat setDateFormat:#"hh:mm a"];
dateFormat.timeZone = [NSTimeZone timeZoneWithName:#"GMT"];
NSDate *date = [dateFormat dateFromString:time];
NSLog(#"date %#",[dateFormat stringFromDate:date]);
NSString *dats1 = #"01:30:00 PM";
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init] ;
[dateFormatter1 setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter1 setDateFormat:#"HH:mm:ss"];
NSDate *date = [dateFormatter1 dateFromString:dats1];
NSLog(#"date : %#", date);
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"hh:mm a"];
NSLog(#"Current Date: %#", [formatter stringFromDate:date]);
I have this:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
[dateFormatter setTimeZone:gmt];
[dateFormatter setDateFormat:#"yyyy-MM-DD"];
NSDate *offsetDay = [dateFormatter dateFromString:entry.date];
String entry.date is 2014-02-16 and resulting offsetDay is 2014-01-16.
Why is this happening??
Thanks.
Give this a try:
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
If I remember correctly, DD is day of the year, not day of the month like you want. You can find a complete NSDateFormatter formatting table here
try this code edit.
NSString *entry=#"2014-01-16";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
[dateFormatter setTimeZone:gmt];
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
NSDate *offsetDay = [dateFormatter dateFromString:entry];
[dateFormatter setDateStyle:NSDateFormatterFullStyle];
NSLog(#"date--%#",[dateFormatter stringFromDate:offsetDay]);
I am using following code to get a date from string but its not showing correct date
NSDateFormatter *saveddf = [[NSDateFormatter alloc] init];
[saveddf setDateFormat:#"EEEE_MMMM dd_YYYY hh:mm a"];
[saveddf setTimeZone:[NSTimeZone systemTimeZone]];
NSDate *savedtdate = [[NSDate alloc] init];
savedtdate = [saveddf dateFromString:datestring];
Saturday_June 22_2013 11:44 AM -- INPUT (datestring).
2013-01-05 06:14:00 +0000 This is output i am getting.(savedtdate).
Please tell whats wrong in my code.
Try this code, it should work:
NSString *yourStringDate = #"Saturday_June 22_2013 11:44 AM";
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:#"EEE_MMM dd_yyyy hh:mm a"];
NSDate *date = [dateFormatter dateFromString: yourStringDate];
//Set the required date format
[dateFormatter setDateFormat:#"dd-MM-yyyy hh:mm"];
NSString *convertedString = [dateFormatter stringFromDate:date];
NSLog(#"Converted String : %#",convertedString);
Try to use this code
NSDateFormatter *formatter;
NSString *dateString;
formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"dd-MM-yyyy HH:mm"];
dateString = [formatter stringFromDate:[NSDate date]];
NSLog(#"%#",dateString);