Convert Date into another format with time - ios

I want to convert a date string "2017-03-25 19:10:00 +0000" in the "2017-03-25 07:10 PM".
But No any luck.Please help me.
Thanks

NSString *dateString = #"2017-03-25 19:10:00 +0000";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"yyyy-MM-dd h:mm a";
NSDate *date = [dateFormatter dateFromString:dateString];
NSString *result = [dateFormatter stringFromDate:date];

Related

dateFromString returns nil. Unable to convert the date to the desired format

This is my input nsstring 30/01/18 3:25 PM
I want the date in this format : 30/Jan/18 3:25 PM.
This is what i tried :
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter setDateFormat:#"dd/MM/yy HH:mm "];
NSDate *date = [[NSDate alloc] init];
date = [dateFormatter dateFromString:releaseDate];
[dateFormatter setDateFormat:#"dd/MMM/yy HH:mm "];
NSString *finalDate = [dateFormatter stringFromDate:date];
Also i tried this :
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd/MM/yy HH:mm z"];
NSDate *date = [[NSDate alloc] init];
date = [dateFormatter dateFromString:releaseDate];
[dateFormatter setDateFormat:#"dd/MMM/yy HH:mm z"];
NSString *finalDate = [dateFormatter stringFromDate:date];
I am always getting date as nil !! What am i doing wrong?
Also : Time comes in both 12 hour format and 24 hour format. How do i handle both at the same time?
You check it below function, its working fine: -
-(void)DateChange
{
NSString *Input_Date =#"31/01/18 04:25 AM";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd/mm/yy hh:mm a"];
NSDate * Format_date = [dateFormatter dateFromString:Input_Date];
[dateFormatter setDateFormat:#"dd/MMM/yy hh:mm a"];
NSString *Change_date = [dateFormatter stringFromDate:Format_date];
NSLog(#"Final Change Date :- %#",Change_date);
}
My Output is :-
Final Change Date :- 31/Jan/18 04:25 AM
Use this function written in Swift to change the date format. Its working not
func dateFormatterfromString(_ date: String!) -> String{
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd-MMM-yyyy"
let date = dateFormatter.date(from:date)!
dateFormatter.dateFormat = "yyyy-MM-dd"
let dateString = dateFormatter.string(from:date)
return dateString
}

coversion of date and time from 31 May 2016 04:30 PM(current format) to 2016-05-31T16:30:00.000+05:30(Required format)

hi can any on help me in achiving this is the string which i have 31 May 2016 04:30 PM(NSSTring) 2016-05-31T16:30:00.000+05:30(Required Format)
NSString *dateString = dateandtime;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateString];
used the following code but returns nil
The current format string does not contain any time zone information, so you have to set the time zone in the date formatter as well as the locale to be able to parse the string independently from the current locale.
The input format is dd MMM yyyy hh:mm a
NSString *dateString = #"31 May 2016 04:30 PM";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.locale = [NSLocale localeWithLocaleIdentifier:#"en_US_POSIX"];
dateFormatter.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:19800];
dateFormatter.dateFormat = #"dd MMM yyyy hh:mm a";
NSDate *dateFromString = [dateFormatter dateFromString:dateString];
dateFormatter.dateFormat = #"yyyy-MM-dd'T'HH:mm:ss.SSSZ";
NSString *stringFromDate = [dateFormatter stringFromDate:dateFromString];
Some how I tried and worked
NSString *myString = dateandtime;
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"dd MMM yyyy hh:mm a";
NSDate *yourDate = [dateFormatter dateFromString:myString];
dateFormatter.dateFormat = #"yyyy-MM-dd'T'HH:mm:ss.SSSZZZZ";
NSLog(#"%#",[dateFormatter stringFromDate:yourDate]);
Whatever you do, test that your code works if the user doesn't use an Indian timezone, and if the user uses 24 hour time instead of AM/PM on their phone. I assume your "dateandtime" was created by converting an NSDate to an NSString; it would be much better to not do that but start with the original NSDate.
NSDateFormatter *dateFormatter=[[NSDateFormatter alloc]init];
NSString *currentDateString = #"2016-05-31T16:30:00.000+05:30";
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSSZZZZ"];
NSDate *currentDate = [dateFormatter dateFromString:currentDateString];
NSLog(#"CurrentDate:%#", currentDate);
you can use this code.

Convert date from weird string to date in format day month and year

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

NSDateFormatter for day and month name

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);

Convert string "2/10/2014 10:12:12 AM" to same format in NSDate in obj c?

I have string #"2/10/2014 10:12:12 AM" (month/day/year hour:minute:second am/pm) in this format. How to convert it into NSDate format.
I feel it easy but not getting in same format.
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = #"MM/dd/yyyy hh:mm:ss a";
NSString *dateString = #"12/24/2014 10:32:42 PM";
NSDate *date = [formatter dateFromString:dateString];
NSLog(#"date: %#", [formatter stringFromDate:date]);
Next thing you'll need to figure out is the time zones.

Resources