I am trying to reformat this NSdate (2016-02-11 01:00:00 +0000 ) into this (11-02-2016).
I have used below code and its returning null.
//newDate format is 2016-02-11 01:00:00 +0000
NSString *dateString = [[self dateFormatter] stringFromDate:newDate];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSDate *date = [dateFormatter dateFromString:dateString];
// Convert date object into desired format
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
NSString *newDateString = [dateFormatter stringFromDate:date];
NSLog(#"people%#",newDateString); //printing null
Easiest way
NSString *strDateFor = #"2016-02-11 01:00:00 +0000";
NSDateFormatter *dateformate=[[NSDateFormatter alloc]init];
[dateformate setDateFormat:#"yyyy-MM-dd HH:mm:ss ZZZZ"];
NSDate *date = [dateformate dateFromString:strDateFor];
NSLog(#"date : %#",date);
NSDateFormatter *dateFormat = [[NSDateFormatter alloc]init];
[dateFormat setDateFormat:#"dd-MM-yyyy"];
NSString *strDate = [dateFormat stringFromDate:date];
NSLog(#"The strDate is - %#", strDate);
The Console output is
date : 2016-02-11 01:00:00 +0000
The strDate is - 11-02-2016
NSString *myString = #"2016-02-11 11:29:04";
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"yyyy-MM-dd HH:mm:ss";
NSDate *yourDate = [dateFormatter dateFromString:myString];
dateFormatter.dateFormat = #"dd-MMM-yyyy";
NSLog(#"%#",[dateFormatter stringFromDate:yourDate]);
+0000 it the time zone definition. You should parse it with Z
try this
[dateformat setDateFormat:#"yyyy-MM-dd HH:mm:ssZZZZ"];
else if your date is current date use
[dateformat setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
Related
Code i have try
NSString *time = #"1470067200000";
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:#"YYYY-MM-dd HH:mm"];
NSDate *date = [NSDate dateWithTimeIntervalSince1970:(NSTimeInterval)([time doubleValue])/1000];
NSString *dateString = [formatter stringFromDate:date];
NSLog(#"%#",dateString);
Result : 2016-08-02 00:00
The HH:mm is always wrong, why?
Set Time Zone in the NSDateFormatter
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:#"yyyy-MM-dd HH:mm"];
[formatter setTimeZone:[NSTimeZone timeZoneWithName:#"UTC"]];
NSDate *date = [NSDate dateWithTimeIntervalSince1970:(NSTimeInterval)([time doubleValue])/1000];
NSString *dateString = [formatter stringFromDate:date];
NSLog(#"%#",dateString);
NSDate always be in UTC and if you apply DateFormatter then default timezone in DateFormatter is localTimeZone. When I run the below code its giving me the below dates. Please check your device/simulator timezone
NSString *time = #"1470067200000";
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:#"YYYY-MM-dd HH:mm"];
// [formatter setTimeZone:[NSTimeZone localTimeZone]];
NSDate *date = [NSDate dateWithTimeIntervalSince1970:(NSTimeInterval)([time doubleValue])/1000];
NSString *dateString = [formatter stringFromDate:date];
NSLog(#"%#",dateString);
Printing description of date:
2016-08-01 16:00:00 +0000
Printing description of dateString: // after From NSDateFormatter
2016-08-01 21:30
To get the stringFromDate in UTC from NSDateFormatter use below line
[formatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"UTC"]];
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
NSDate *date = [NSDate date];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"hh:mm a"];
[df setTimeZone:[NSTimeZone systemTimeZone]];
[df setLocale:[NSLocale systemLocale]];
NSLog(#" >> %# >>> %#",date,[df dateFromString:#"12:18 AM"])
And log is
>> 2014-04-10 07:00:48 +0000 >>> 2000-01-01 06:52:00 +0000
I want that the second date has also the same date as the first.
like
2014-04-10 07:00:48 +0000 >>> 2014-04-10 06:52:00 +0000
If you want to convert the time string "12:18 AM" to a date for the current day then you can proceed as follows:
NSString *time = #"12:18 AM";
NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
[fmt setLocale:[NSLocale localeWithLocaleIdentifier:#"en_US_POSIX"]];
// Get year-month-day for today:
[fmt setDateFormat:#"yyyy-MM-dd "];
NSString *todayString = [fmt stringFromDate:[NSDate date]];
// Append the given time:
NSString *todaysTime = [todayString stringByAppendingString:time];
// Convert date+time string back to NSDate:
[fmt setDateFormat:#"yyyy-MM-dd h:mm a"];
NSDate *date = [fmt dateFromString:todaysTime];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd/MM/yyyy hh:mm:ss a"];
By using this formatter, you can get NSDate