Convert integer to a date string - iphone

In Objective-C: Is there a simple way, as in Excel and SAS, to convert an integer into a datetime string?
In Excel, you'd format to "mmm dd, yyyy hh:mm:ss".
For instance, if I had the integer:
1328062560
and I want the output to be:
2012-01-31 21:16:00
or even (as in Excel):
Jan 31, 2012 21:16:00
PS: I don't want to be too demanding, but I'd like it simple and inline for use in NSLog, so I can write simply for debugging
NSLog("The time as an integer is %d and as a date %mmmddyyyyhh:mm:ss", [array objectAtIndex: i], [array objectAtIndex: i]);

You can get an NSDate object like this:
NSDate *date = [NSDate dateWithTimeIntervalSince1970:1328062560];
Then, if you don't care about the exact format of the string, you can just do this:
NSString *s = [date description];
That will give you a string like “2012-02-01 02:16:00 +0000”.
If you want a specific string format, use an NSDateFormatter.

You can use NSDateFormatter to do this. You first make a string from your integer value:
NSString *dateString = [NSString stringWithFormat:#"%d", integerDate];
And then create an NSDateFormatter with the corresponding format:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[myDateFormatter setDateFormat:#"yyyyMMddhhmm"]; // Change to suit your format
NSDate *dateFromString = [myDateFormatter dateFromString:dateString];
You can then retrieve the date as a string in the format you want:
[myDateFormatter setDateFormat:#"yyyy/MM/dd hh:mm"]; // Change to suit your format
NSString *stringFromDate = [formatter stringFromDate:dateFromString];

Assuming that integer is a time in seconds since the epoch, you can use NSDate's initWithTimeIntervalSince1970 or dateWithTimeIntervalSince1970 methods to create an NSDate object with the date you care about, and then use NSDateFormatter to pretty it up for display.

Related

Conversion from NSString to NSDate doesn't work

I have to convert this NSString: "12/13/1980" to a NSDate object.
I use a code like this:
NSString *birthday = #"12/13/1980"
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"MM/dd/yyyy"];
NSDate *date = [dateFormat dateFromString:dateString];
But the result is this: "1980-12-12 23:00:00 +0000"
Why? I'd like the same format and the same date.
Thank you very much.
The [dateFormat dateFromString:dateString] method produces an NSDate object which represents a single point in time.
When you NSLog a date object, it is printing a system representation of the NSDate object. I'm assuming your locale is GMT+1 .. so the NSLog prints 12/12/1980 23:00.
If you want to print the date object back, use your formatter to do [dateFormat stringFromDate:date];
It's because a NSDate object has always to have a time and a timezone, so if you don't specify that in your the string you're trying to convert IOS will use your local timezone and then guess the time, wich in this case will be 23:00 in UTC 0
The format is dependent on the localisation, there's also the option to set whether the date/time is short, medium or long format - I believe short is what you're looking for:
[dateFormatter setDateStyle:NSDateFormatterShortStyle];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/Reference/Reference.html

NSString with date: reduce size of date

I have a NSString like Mon Feb 06 20:02:17 +0000 2012. I want to write it in a shorter way, maybe like: DD-MM-YY, HH:MM. I have think that maybe I can convert it to NSDate and again to a shorter NSString, but I don't know how to convert this strange date format to NSDate.
If you need more information, ask me.
Thanks!
You need to use an NSDateFormatter to tell NSDate the format the original date is in and the format you want it in, then you can convert between the two like this:
NSString *oldDate = #"Mon Feb 06 20:02:17 +0000 2012";
NSString *oldFormat = #"EEE MMM dd HH:mm:ss ZZ yyyy";
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:oldFormat];
NSDate *date = [dateFormatter dateFromString:sourceString];
NSString *newFormat = #"dd-MM-yy, HH:mm";
[dateFormatter setDateFormat:newFormat];
NSString *newDate = [dateFormatter stringFromDate:date];
There are similar question and answers in StackOverflow and a tutorial that may help you:
Tutorial:
http://iosdevelopertips.com/cocoa/date-formatters-examples-take-3.html
Similar questions:
NSString to NSDate
Convert NSString->NSDate?
How to convert NSDate to NSString?
Convert NSString of a date to an NSDate
EDIT
NSString *dateStr = #"20081122";
// Convert string to date object
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyyMMdd"];
NSDate *date = [dateFormat dateFromString:dateStr];
// Convert date object to desired output format
[dateFormat setDateFormat:#"EEEE MMMM d, YYYY"];
dateStr = [dateFormat stringFromDate:date];
Try with NSDateFormatter.
You can convert to NSDate using one of its method:
- (NSDate *)dateFromString:(NSString *)string
And then convert that to whatever you want using NSDateFormatter.
Edit: Actually NSDate has a class method so you may check that as well:
+ (id)dateWithString:(NSString *)aString

null value generates while converting the date from one format to another format

I am having a date format "16-11-2011". but I want to convert it into "16 Nov"
I written the code for it but null value generates instead of the format "16 Nov"
for this I have written following code
NSDate *checkIn = [[NSUserDefaults standardUserDefaults]valueForKey:#"CHECK_1"];
NSLog(#"Date: %#",checkIn);
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"dd MMM"];
NSString *checkInDate = [formatter stringFromDate:checkIn];
NSLog(#"checkInDate:%#",checkInDate);
and the results display by nslog statements are as follows:
Date: 16-11-2011
checkInDate:(null)
so I am not able to convert it into the format which I want
plz help me to solve the problem.
I suspect that checkIn is a NSString not NSDate.
Therefore, you would have to convert it to a NSDate first and then convert it back to the desired string format.
NSString *checkIn = [[NSUserDefaults standardUserDefaults]valueForKey:#"CHECK_1"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
NSDate *checkInTempDate = [dateFormatter dateFromString:checkIn];
[dateFormatter setDateFormat:#"dd MMM"];
NSString *checkInDate = [dateFormatter stringFromDate:checkInTempDate];
The problem is in the first line:
NSDate *checkIn = [[NSUserDefaults standardUserDefaults]valueForKey:#"CHECK_1"];
NSUserDefaults does not have a method valueForKey, it is Key Value Coding" that is returning a value. NSUserDefaults also does not handle dates. What you are getting back from NSUserDefaults is a string, by a quirk NSDate is accepting that.
The last 4 lines of code are correct but need a date, checkIn is a string.
To test this put this line of code after the first line, ignore any incompatible type warning):
NSLog(#"class name: %s", class_getName([checkIn class]));

iphone NSDate Problem

Following code I can use for storing the value of UILabel into a string. And Value of string is store into NSDate.
NSString *star = [[NSString alloc]init];
star = lbtInDate.text;
NSString *end = [[NSString alloc]init];
end = lblOutDate.text;
NSDateFormatter *dateFormatter1=[[NSDateFormatter alloc]init];
[dateFormatter1 setDateFormat:#"yyyy-MM-dd HH:mm"];
NSDate *sdate= [dateFormatter1 dateFromString:star];
NSDate *edate=[dateFormatter1 dateFromString:end];
but the value of sdate and edate is shows nil. so please help how can i store the value of UILabel into NSDate for compare.
Your code is perfectly valid only the lbtInDate.text; and lblOutDate.text; respect the date format. If not your NSDate's will be null.
If lblOutDate.text; is something like #"2002-12-23", your date format should be #"yyyy-MM-dd" and so on.
Data Formatting Guide

Getting NSDate from NSString

I am trying to convert NSString to NSdate. The string has date in it.i use the following
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"MM/dd/yyyy"];
NSDate *date = [dateFormat dateFromString:setitempricedate];
NSLog(#"Date : %#",date);
the input string contains the date in the format 02/08/2011
when i log date , i get 2011-02-07 18:30:00 GMT,
I want to get the date as 02/08/2011 only. Where am i going wrong
In your code, you are asking the date formatter to create a date object for you from a given string. Then you printed out that date object. What you want is to create that date object, then ask the date formatter to format that date object you just created. You should be calling stringFromDate instead.
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"MM/dd/yyyy"];
NSDate *date = [dateFormat dateFromString:setitempricedate];
NSLog(#"Date: %#", [dateFormat stringFromDate:date]);
You will not be able to change the date representation. These (NSDateFormatter, NSCalendar) classes are provided to get formatted strings not date.
Whenever you will have an instance of NSDate class it will be in same format like you are getting.
2011-02-07 18:30:00 GMT
If you want custom styles better you go with NSString
You are printing the actual date object which doesn't follow the date format you specified. You could do something like
[[NSDate dateWithString:setitempricedate] stringFromDate];