I have a string #"21:57" and I want to save it with the same HH:mm format in NSDate. I tried converting it using:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
dateFormatter.dateFormat = #"HH:mm";
NSDate *date = [dateFormatter dateFromString:stringToConvertFrom];
but I'm getting 2000-01-01 21:57:00 +0000 instead of 21:57.
Related
I am storing dates as strings in the format: 2018-07-10 21:00:29 +0000
I created a formatter to convert the strings back into dates:
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
formatter.dateFormat = #"yyyy-MM-dd'T'HH:mm:ss.SSSZ";
[formatter setLocale:[NSLocale currentLocale]];
NSDate *date = [formatter dateFromString:[chore getDate]];
However, the date that is returned is always nil. Does anyone know what I'm doing wrong?
You set the wrong format in the dateformat. So it will always return the nil value.
The following method can return the date in the NSDate format.
-(NSDate *)convertDate :(NSString *)date1 //the argument date1 is the string date you used
{
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
formatter.dateFormat = #"yyyy-MM-dd HH:mm:ss Z"; // the format which you used in the string date
[formatter setLocale:[NSLocale currentLocale]]; //This for set the Locale .It is not compulsory.
NSDate *date = [formatter dateFromString:date1];
return date;
}
Refer to date formatter date formats here:
You need to update your date format to something like:
"yyyy-MM-dd HH:mm:ss Z"
My string is strDate:04/12/2016 on converting it to date in format dd/MM/yyyy. It is producing another format.it is producing result as weekEndDate:2016-12-04 00:00:00 +0000. My code is
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"dd/MM/yyyy"];
weekEndDate=[formatter dateFromString:strDate];
2016-12-04 00:00:00 +0000
This is not a wrong format, whenever you will print an object of NSDate in your debugger that object will be printed like the above string i.e. in UTC timezone, if you want to check correct format then convert this date object in string and print that string you will get correct format.
It's normal that you get that format, if you want to get your weekend date using that formatter here is how you do it :
NSString *strDate = #"04/12/2016";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"dd/MM/yyyy"];
NSDate *weekEndDate = [formatter dateFromString:strDate];
NSLog(#"%#", [formatter stringFromDate:weekEndDate]); // 04/12/2016
NSLog(#"%#", weekEndDate); // 2016-12-04 00:00:00 +0000
i have date in this format 2014-09-20 04:45:20 +0000 and i need to convert this to yyyy-MM-dd'T'HH:mm:ss.SSSZ format.I tried with the following code
NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"];
NSDate *date = [formatter dateFromString:#"2014-09-20 04:45:20 +0000"];
NSLog(#"%#",date);
but date giving me null value..
I am geting 2014-09-20 04:45:20 +0000 from time picker so anythg needs to be done on picker side will also help me.
Try this:
NSString *aStrDate = #"2014-09-20 04:45:20 +0000";
NSDateFormatter *aDateFormatter = [[NSDateFormatter alloc] init];
aDateFormatter.dateFormat = #"yyyy-MM-dd HH:mm:ss Z";
NSDate *aDate = [aDateFormatter dateFromString:aStrDate];
aDateFormatter.dateFormat = #"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
NSLog(#"%#",[aDateFormatter stringFromDate:aDate]);
I can't parse date from string via NSDateFormatter. The date string is #"2014-01-21T20:00:36+04:00". I am trying to use this format string: #"yyyy-MM-ddTHH:mm:ss" but it doesn't work. Please help me.
The full code is:
NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"yyyy-MM-ddTHH:mm:ssZ";
NSDate * date = [dateFormatter dateFromString:dateString];
add Z at the end #"yyyy-MM-dd'T'HH:mm:ssZZZZZ" as you have timezone included
Following on from http://unicode.org/reports/tr35/tr35-6.html#Date_Format_Patterns
NSString *dateString = #"2014-01-21T20:00:36+04:00";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
dateFormat.dateFormat = #"yyyy-MM-dd'T'HH:mm:ssZZZZZ";
NSDate *date = [dateFormat dateFromString:dateString];
NSLog(#"Parsed date is %#", date);
yields the following output which seems correct
Parsed date is 2014-01-21 16:00:36 +0000
I have weird result trying to output NSDate object from NSString.
My NSString is: 1976-06-11
My method to convert is:
-(NSDate*)dateFromString:(NSString *)dateString{
// Convert string to date object
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd"];
NSDate *date = [dateFormat dateFromString:dateString];
return date;
}
But it output 1976-06-10 21:00:00 +0000
How could that happen? Difference in 1 day.
You have date in UTC format. Use this code to converting your date to local time:
NSTimeInterval seconds; // assume this exists
NSDate *ts_utc = [NSDate dateWithTimeIntervalSince1970:seconds];
NSDateFormatter *utcFormatter = [[NSDateFormatter alloc] init];
utcFormatter.timeZone = [NSTimeZone timeZoneWithName:#"UTC"];
utcFormatter.dateFormat = #"yyyy.MM.dd G 'at' HH:mm:ss zzz";
NSDateFormatter *localFormatter = [[NSDateFormatter alloc] init];
localFormatter.timeZone = [NSTimeZone timeZoneWithName:#"EST"];
localFormatter.dateFormat = #"yyyy.MM.dd G 'at' HH:mm:ss zzz";
NSString *utcDateString = [utcFormatter stringFromDate:ts_utc];
NSString *LocalDateString = [localFormatter stringFromDate:ts_utc];
Or you can use [NSTimeZone defaultTimeZone] to prevent hardcoded strings for timezone names. This method returns the system time zone, If no default time zone has been set.
You can use following methods to convert an UTC date string into UTC date and local date
- (NSDate *)convertIntoGMTZoneDate:(NSString *)dateString
{
NSDateFormatter *gmtFormatter = [[NSDateFormatter alloc]init];
[gmtFormatter setDateStyle:NSDateFormatterFullStyle];
[gmtFormatter setTimeStyle:NSDateFormatterFullStyle];
[gmtFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"GMT"]];
return [gmtFormatter dateFromString:dateString];
}
- (NSDate *)convertIntoSystemZoneDate:(NSString *)dateString
{
NSDateFormatter *systemZoneFormatter = [[NSDateFormatter alloc]init];
[systemZoneFormatter setDateStyle:NSDateFormatterFullStyle];
[systemZoneFormatter setTimeStyle:NSDateFormatterFullStyle];
[systemZoneFormatter setTimeZone:[NSTimeZone systemTimeZone]];
return [systemZoneFormatter dateFromString:dateString];
}
func dateFromString(dateString: String) -> NSDate {
// Convert string to date object
var dateFormat = NSDateFormatter()
dateFormat.dateFormat = "yyyy-MM-dd"
dateFormat.timeZone = NSTimeZone(name: "UTC")
var date = dateFormat.dateFromString(dateString)!
print(date)
return date
}
output :
1976-06-11 00:00:00 +0000
If you debug code, it shows 1 day difference but after run you will find the actual date which is you enter.
It works for me.I think it will helps you.
Thank you