I am trying to convert a NSString into a double using the following:
double lastSynced;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"YYYY-MM-DD HH:MM:SS"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:#"2012-03-28 18:41:25"];
lastSynced = [dateFromString timeIntervalSince1970];
However when printing lastSynced it always gives me 0. Why is this?
Your date format is wrong. Should be:
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
You have to do:
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
instead of:
[dateFormatter setDateFormat:#"YYYY-MM-DD HH:MM:SS"];
Good luck!
Your formatting strings are not correct. You should use :
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
Good luck !
Related
I know this is a simple question, but im dealing with this problem for almost 6 hours. I want to order a date to another format. Not a big deal, but it still gives me back: (null)
Example:
Date I have:
2013-06-01 10:54:42
Date I want to be displayed:
01-06-2013 10:54:42
My code:
NSString *dateString = #"2013-06-01 10:54:42";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSDate *date = [dateFormat dateFromString:dateString];
[dateFormat setDateFormat:#"dd-MM-yyyy HH:mm:ss"];
NSLog(#"%#",date);
Any help would be appreciated!
Try:
NSLog(#"%#",[dateFormat stringFromDate:date]);
You are creating the formatter but not using it to create your date string. Instead you're just printing the date object directly and its description method is printing it out with a default formatting, not the formatter you're trying to use.
Your code works fine
NSString *dateString = #"2013-06-01 10:54:42";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSDate *date = [dateFormat dateFromString:dateString];
[dateFormat setDateFormat:#"dd-MM-yyyy HH:mm:ss"];
NSLog(#"%#",date);
NSLog(#"%#",[dateFormat stringFromDate:date]);
Either way it works for me. Must be something in the actual code you have the problem with. The code you show is obviously prepared for this question:
NSString *dateString = #"2013-06-01 10:54:42";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSDate *date = [dateFormat dateFromString:dateString];
[dateFormat setDateFormat:#"dd-MM-yyyy HH:mm:ss"];
NSLog(#"%#",date);
NSLog(#"%#",[dateFormat stringFromDate:date]);
Output:
2013-06-01 19:54:19.497 testpgm[23606:c07] 2013-06-01 08:54:42 +0000
2013-06-01 19:54:21.452 testpgm[23606:c07] 01-06-2013 10:54:42
I try to get and manage date with NSDateFormatter but how to make it works.
I think it's a problem with my first "dateFormatter" which have not the right format, because when I use the second formatter with function "stringFromDate" and paramter "[NSDate date]" it works well, but not when I use my NSDate "dateFromString".
//"dateString" have this format: "2011-05-26T16:18:26Z"
NSString *dateString = [[tracks objectAtIndex:0] objectForKey:#"to-date"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE, MMM d, yyyy"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateString];
[dateFormatter release];
dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE, MMM d, yyyy"];
//NSString *toDate = [dateFormatter stringFromDate:dateFromString];
NSString *toDate = [dateFormatter stringFromDate:[NSDate date]];
Thank you for your help :)
Well of course, the DateFormat is not the same as the one you are receiving from the server.
Try this:
dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss Z"];
NSDate *date = [dateFormatter dateFromString:[dateString stringByReplacingOccurrencesOfString:#"Z" withString:#" +0000"]];
Assuming that to-date is a string and not already an NSDate try this instead:
//"dateString" have this format: "2011-05-26T16:18:26Z"
NSString *dateString = [[tracks objectAtIndex:0] objectForKey:#"to-date"];
NSDate *dateFromString = [[NSDate dateFromString:dateString];
If you want to display a date to the user formatted in a particular format you can then use set up an NSDateFormatter and use stringFromDate: to get a string to show the user. If it still doesn't display correctly set the calendar (I forget if you always have to set it or if there is a default).
I have two strings:
date1 = 3-5-2014;
date2 = 4-2-2010;
I have to convert them in date and then compare them. I want the same format of date as in strings i.e., dd-mm-yyyy. How it can be done?
NSString *string=#"03-05-2014";
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
NSDate *dateFromString = [dateFormatter dateFromString:string];
enjoy...
You can extract NSDate objects from strings using the NSDateFormatter class. See the Date Formatters section Apple's Data Formatting Guide for a detailed explanation and sample code.
Try with below
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"MM/dd/yyyy h:mm a"];
NSDate *date = [dateFormat dateFromString:dateStr];
[dateFormat release];
here is the SO post
Convert NSString date to NSDate
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
NSDate *date = [dateFormatter dateFromString:#"25-8-2010"];
NSLog(#"%#", date);
[dateFormatter release];
Hope this works for you
Cheers :)
In my app I DO NOT want to use the local time on the phone. I want to get the current time in GMT. To do this I have a date formatter with time zone set to GMT. I then do the following to get back the current time:
NSString *dateOne = [df stringFromDate:[NSDate date]];
NSDate *date1 = [df dateFromString:dateOne];
My app gets an EXC_BAD_ACCESS though on the dateFromString call. I checked the returned value from dateOne and it's format matches the dateformatters. What am I doing wrong? Is there a better way to do this?
Thanks
EDIT:
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"yyyy-mm-dd HH:mm:ss"];
An easier way perhaps:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-mm-dd HH:mm:ss"];
NSTimeZone *tz = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
[dateFormatter setTimeZone:tz];
NSDate *date = [NSDate date];
NSString *dateString = [dateFormatter stringFromDate:date];
NSLog(#"Time now in GMT: %#",dateString);
I have a NSString which is passed from an xml feed........
NSString *strDate =#"May 14 2010";
I'm currently using this code to format the date........
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
[dateFormatter setDateFormat:#"d:MMM"];
NSDate *myDate = [dateFormatter dateFromString:strDate];
NSLog(#"%#", [dateFormatter stringFromDate:myDate]);
I want to format my string only to display day and month and currently I'm getting a nil value to the NSDate. Can anyone please help me with this?......
Thanks.
I think you're slightly misunderstanding how NSDateFormatter works. It cannot automatically work out what format the date in your string is in.
If you change your code to something like this:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MMM dd yyyy"];
NSDate *myDate = [dateFormatter dateFromString:strDate];
[dateFormatter setDateFormat:#"d:MMM"];
NSLog(#"%#", [dateFormatter stringFromDate:myDate]);
It should do what you want.