0
votes

I want to change my date format from "MM/dd/YYYY HH:mm:ss" to "EEE dd MMM yyyy hh:mm a", but the code I am using not working for example if my input is 11/30/2016T01:04:30 I am getting the month changed as December, can any one help where is the mistake?

NSString * date = @"11/30/2016T01:04:30";
date = [date stringByReplacingOccurrencesOfString:@"T" withString:@" "];

NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"MM/dd/YYYY HH:mm:ss"];
NSDate *myDate = [df dateFromString: date];

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"EEE dd MMM yyyy hh:mm a"];
NSString *dayName= [dateFormatter stringFromDate:myDate];
NSLog(@"the converted Day  %@",dayName);
1
what you want out put just tell meHimanshu Moradiya
@HimanshuMoradiya want to convert --- MM/dd/YYYY HH:mm:ss-- format to --EEE dd MMM yyyy hh:mm a --Arun
@Arun You are facing this issue because of capital YYYY in first date Formate it should be small yyyy Also there is no need to use two dateFormatter you can work with single once and instead of replacing T with space simply change your dateFormate to MM/dd/yyyy'T'HH:mm:ssNirav D
@NiravD tanks it working nowArun
@Arun Welcome mate :)Nirav D

1 Answers

2
votes

no need of this

// date = [date stringByReplacingOccurrencesOfString:@"T" withString:@" "];

use like

 NSDateFormatter *df = [[NSDateFormatter alloc] init];
 NSString * date = @"11/30/2016T01:04:30";

[df setDateFormat:@"MM/dd/yyyy'T'HH:mm:ss"];
NSDate *myDate = [df dateFromString: date];

[df setDateFormat:@"EEE dd MMM yyyy hh:mm a"];
NSString *dayName= [df stringFromDate:myDate];
NSLog(@"the converted Day  %@",dayName);

Output:

the converted Day  Wed 30 Nov 2016 01:04 AM