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
Related
I getting the date from the webservice like "2012-07-03 07:26:48". I have saved in NSString after parsing from webservice. While convert the NSString to NSDate used below code,
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
[dateFormatter1 setDateFormat:#"yyyy-MM-dd hh:mm:ss"];
NSString *dateStr = #"2012-07-03 07:26:48";
NSDate *date = [dateFormatter1 dateFromString:dateStr];
NSLog(#"Date : %#", date);
The NSLog value is "2012-07-03 01:56:48 +0000";
The NSLog value is "2012-07-03 01:56:48 +0000"; I don't know why the time has changed. Can anyone please help me to do this?
You need to put timezone as:
theDateFormatter.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
Because it is considering GMT time format.
You have to set the time zone of the NSDateFormatter.
By default,Date is saved in utc format while converting string to nsdate... you can get original time from date as a string..if you want to change timezone, change it using dateformatter.. otherwise dont change
im using a string to convert it to an nsDate
is working fine,
but when i try to add time to my string I get null on the logs
NSString *dateStringus = [NSString stringWithFormat:#"12-09-2011"];
NSLog(#"%#",dateStringus);
NSDateFormatter *dateFormattero = [[NSDateFormatter alloc] init];
[dateFormattero setDateFormat:#"dd-MM-yyyy"];
NSDate* d = [dateFormattero dateFromString:dateStringus];
NSLog(#"tomad tu string date ::%#",d);
NSString *pingus = [dateFormattero stringFromDate:d];
NSLog(#"transumatada ::%#", pingus);
NSLog(#"\n");
Works fine for only date,
but
if I do
NSString *dateStringus = [NSString stringWithFormat:#"12-09-2011 14:14:00"];
I get null on the logs
how to fix this string to include time? thanks!
The dateFormat property of the NSDateFormatter dateFormattero must match the format of date/time in the string dateStringus.
Change:
[dateFormattero setDateFormat:#"dd-MM-yyyy"];
Into:
[dateFormattero setDateFormat:#"dd-MM-yyyy HH:mm:ss"];
Here is a good reference on date format patterns.
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.
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]));
I developing an application, in which i found a ridiculous problem in type casting, I am not able to type cast NSDate to NSString.
NSDate *selected =[datePicker date];
NSString *stringTypeCast = [[NSString alloc] initWithData:selected
encoding:NSUTF8StringEncoding];
From ,above snippet datePicker is an object of UIDatePickerController.
One method would be:
NSString *dateString = [NSString stringWithString:[selected description]]
See the documentation.
Another would be:
NSString *dateString = [NSString stringWithFormat:#"%#", selected]
See the documentation.
A more appropriate method would be:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSString *dateString = [dateFormatter stringFromDate:selected];
[dateFormatter release];
This will automatically return the date in a string formatted to the user's local date format. See the documentation.
Use NSDateFormatter to convert NSDate objects to NSString objects. Type conversion is different from type casting.
You don't want to do this. What you really want to do is to use an NSDateFormatter to properly convert the NSDate into an NSString. Going about this any other way is Not Correct™.
The method you use requires NSData, not NSDate, that's why it doesn't work.