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);
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
Related
Hi I am parsing an end point of API, from there I am getting a string which contain like this date string 2016-01-18T13:28:06.357 but I want to convert this like 01 Jan 16, I have tried many times by using different format one of following mentioned. kindly need suggestion how to accomplish this task.
NSString * date = [dictionary objectForKey:#"date"];
NSDateFormatter *dateformat = [[NSDateFormatter alloc] init];
[dateformat setTimeZone:[NSTimeZone timeZoneWithName:#"EST"]];
[dateformat setDateFormat:#"dd MMM yyyy"];
NSDate *myDate = [dateformat date];
// Objective C
NSString * date = [dictionary objectForKey:#"date"];
NSDateFormatter *dateformat = [[NSDateFormatter alloc] init];
[dateformat setTimeZone:[NSTimeZone timeZoneWithName:#"EST"]];
[dateformat setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSS"];
NSDate *myDate = [dateformat dateFromString:date];
[dateformat setDateFormat:#"dd MMM yyyy"];
NSString *strDate=[dateformat stringFromDate:myDate];
// Swift 3.0
var date = (dictionary["date"] as! String)
var dateformat = DateFormatter()
dateformat.timeZone = NSTimeZone(name: "EST")
dateformat.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS"
var myDate = dateformat.date(fromString: date)!
dateformat.dateFormat = "dd MMM yyyy"
var strDate = dateformat.string(from: myDate)
You should use this format:
NSString *dateStr = #"2016-01-18T13:28:06.357";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSS"];
NSDate *date = [formatter dateFromString:dateStr];
// date is 2016-01-18 08:28:06 +0000
And next convert that date to format you need
You can also use this :
NSString *dateString = #"2016-01-18T13:28:06.357";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"];
NSDate *date = [dateFormatter dateFromString:dateString];
// Convert date object into desired format
[dateFormatter setDateFormat:#"dd MMMM yy"];
NSString *newDateString = [dateFormatter stringFromDate:date];
NSLog(#"newDateString=%#",newDateString);
You should use this format code
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
//Get Date
[formatter setDateFormat:#"dd"];
NSString *strDate = [formatter stringFromDate:[NSDate date]];
NSLog(#"Date is - %#",strDate);
//Get Month
[formatter setDateFormat:#"MMM"];
NSString *strMonth = [formatter stringFromDate:[NSDate date]];
NSLog(#"Month is - %#",strMonth);
//Get Year
[formatter setDateFormat:#"yyyy"];
NSString *strYear = [formatter stringFromDate:[NSDate date]];
NSLog(#"Year is - %#",strYear);
Log is :
Date is - 05
Month is - Feb
Year is - 2016
for set more format, Click Me
I am trying to get date in the format Monday, March 09, 2015. But following code is not returning required date format. I think I am using wrong Formatter. Here is the code:
NSString *dateString = #"09-03-2015";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd MMMM yyyy"];
NSDate *date = [[NSDate alloc] init];
date = [dateFormatter dateFromString:dateString];
NSLog(#"%#",[dateFormatter stringFromDate:date]);
try this...
//Getting date from string
NSString *dateString = #"09-03-2015";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
NSDate *date = [[NSDate alloc] init];
date = [dateFormatter dateFromString:dateString];
// converting into our required date format
[dateFormatter setDateFormat:#"EEEE, MMMM dd, yyyy"];
NSString *reqDateString = [dateFormatter stringFromDate:date];
NSLog(#"date is %#", reqDateString);
LOG:2015-03-09 12:40:33.456 TestCode [1377:38775] date is Monday, March 09, 2015
Firstly you must convert your dateString to NSDatevalue
NSString *dateString = #"09-03-2015";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
NSDate *date = [[NSDate alloc] init];
date = [dateFormatter dateFromString:dateString];
NSLog(#"%#",[dateFormatter stringFromDate:date]);
after that you could use this format: #"EEEE,MMMM dd, yyyy" to convert that dateValue to dateString like Monday, March 09, 2015
Hope this help
Helpful link for you
Try to set dateFormat property to #"dd'-'MM'-'yyyy" . Always check the unicode the date symbol table. "MMMM" means that the date month should be a full month name.
Try this one..
NSString * yourJSONString = #"09-03-2015";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];;
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
[dateFormatter setLocale:[NSLocale currentLocale]];
NSDate *dateFromString = [dateFormatter dateFromString:yourJSONString];
[dateFormatter setDateFormat:#"EEEE,LLLL dd, yyyy"];
NSString *output = [dateFormatter stringFromDate:dateFromString];
NSLog(#"%#", output);
Try this:-
NSDate *date = [NSDate date];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
NSString * format = [NSDateFormatter dateFormatFromTemplate:#"EEEEMMMMdyyyy" options:0 locale:[NSLocale currentLocale]];
[formatter setDateFormat:format];
NSString *dateFormatted = [fullDateFormatterTime stringFromDate: date];
NSLog(#"Formatted date: %#", dateFormatted);
I have tried this:
NSString *myDateString = #"29 Feb 2016 20:33:09";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd MMM yyyy"];
NSDate *myDate = [dateFormatter dateFromString:myDateString];
NSLog(#"%#", myDate);
NSString *myDateString = #"29 Feb 2016 20:33:09";
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd MM yyyy HH:mm:ss"];
NSDate* myDate = [dateFormatter dateFromString:myDateString];
NSLog(#"%#", myDate);
Your convert date nil because you didn't use right dateFormat
try :
[dateFormatter setDateFormat:#"dd MMM yyyy HH:mm:ss"];
more infomation
For your required Format,Use this:-
[dateFormatter setDateFormat:#"dd-MM-yyyy HH:mm:ss"];
NSString *myDateString = #"29 Feb 2016 20:33:09";
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"dd MMM yyyy HH:mm:ss";
NSDate *date = [dateFormatter dateFromString:myDateString];
dateFormatter.dateFormat = #"dd-MM-yyyy HH:mm:ss";
NSString *myDate = [dateFormatter stringFromDate:date];
NSLog(#"%#",myDate);
NSString *myDateString = #"29 Feb 2016 20:33:09";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd MMM yyyy HH:mm:ss"];
NSDate *myDate = [dateFormatter dateFromString:myDateString];
//here i get correct date ok But
NSLog(#"%#", myDate);
NSString *ss=[NSString stringWithFormat:#"%#",myDate] ;
NSArray *components = [ss componentsSeparatedByString: #"+"];
NSString *StartnewDateString = [components objectAtIndex:0];
//Here time is changed of StartnewDateString
I want parse date strings from API with time zone PET. So I created NSDateFormatter and converting the string into date but unfortunately its not working. I am getting nil as the result. Any workarounds ?
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"UTC"]];
[formatter setDateFormat:#"EEE MMM dd HH:mm:ss yyyy zz"];
NSDate *newStartDate = [formatter dateFromString:#"Mon Sep 29 14:40:00 2014 PET"];
NSLog(#"newStartDate - %#",newStartDate);
Try with the below function.It will Work...
NSDate *newStartDate = [self dateFromString:#"Mon Sep 29 14:40:00 2014 PET"];
NSLog(#"newStartDate - %#",newStartDate);
- (NSDate *)dateFromString:(NSString*)inDateString
{
NSDateFormatter *pivotalDateFormatter = [[NSDateFormatter alloc] init];
NSDate * dateToBeReturned = nil;
[pivotalDateFormatter setDateFormat:#"EEE MMM dd HH':'mm':'ss yyyy z"];
NSString * timezoneAbbreviation = [[inDateString componentsSeparatedByString:#" "] lastObject];
NSTimeZone *timeZone = [NSTimeZone timeZoneWithAbbreviation:timezoneAbbreviation];
if (timeZone)
{
NSString *gmtTime = [inDateString stringByReplacingOccurrencesOfString:timezoneAbbreviation withString:#"GMT"];
dateToBeReturned = [[pivotalDateFormatter dateFromString:gmtTime] dateByAddingTimeInterval:-timeZone.secondsFromGMT];
}
return dateToBeReturned;
}
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSDate *newStartDate = [NSDate date];
NSString *newStartDateStr = [dateFormatter stringFromDate:newStartDate];
now change the dateformatter dateformat
[dateFormatter setDateFormat:#"EEE MMM dd HH:mm:ss yyyy zz"]; //set dateformat as per ur requirement
newStartDate = [dateFormatter dateFromString:newStartDateStr];
NSLog(#"%#", newStartDate);
u will get the dateformat u required
happy coding
I need to take the NSString Dec 4, 2012, 12:33 PM and convert it to separate out the month, day, and year, so that I can have 3 different strings of 12, 04, and 2012.
I figure that I should convert the NSString to NSDate and then reformat the date to change out NSString, but am running into issues.
I have:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MMM dd, yyyy, hh:mm p"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:substring];
NSLog(#"Date%#", dateFromString);
[dateFormatter release];
However, the date keeps coming back null.
The problem is with your locale I think.
You should try to print how your dateFormatter formats current date [NSDate new].
It works for me:
NSString* substring = #"Dec 12 2012 12:08 PM";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US"];
[dateFormatter setDateFormat:#"MMM d yyyy h:mm a"]; // not 'p' but 'a'
NSDate *dateFromString = [dateFormatter dateFromString:substring];
Convert
[dateFormatter setDateFormat:#"MMM dd, yyyy, hh:mm p"];
to, Because MMM dd, yyyy, hh:mm p not a valid date formate
[dateFormatter setDateFormat:#"MMM dd, yyyy, hh:mm a"];
And
NSDate *dateFromString = [dateFormatter dateFromString:substring];
To get string again
NSLog(#"%#",[dateFormatter stringFromDate:dateFromString]);
for me NSLog is Dec 04, 2012, 12:33 PM
#import "NSString+Date.h"
#implementation NSString (Date)
+ (NSDate*)stringDateFromString:(NSString*)string;
{
NSString *dateString = #"01-02-2010";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
// this is imporant - we set our input date format to match our input string
// if format doesn't match you'll get nil from your string, so be careful
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
NSDate *dateFromString = [[NSDate alloc] init];
// voila!
dateFromString = [dateFormatter dateFromString:dateString];
return dateFromString;
}
+(NSString*)StringFromDate :(NSDate*)date;
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
NSString *stringDate = [dateFormatter stringFromDate:[NSDate date]];
NSLog(#"%#", stringDate);
return stringDate;
}
` #end