Getting NSDate from Seconds in ObjC [duplicate] - objective-c

This question already has answers here:
NSTimeInterval to NSDate
(7 answers)
Closed 9 years ago.
I have got seconds as 54000.
How can i get the time as 3:00:00 PM ?
Any dateformatter's i can use to get it ?
Thanks

Assuming given seconds are in local time zone and you want to convert to local time zone.
NSInteger timeInSeconds = 54000;
NSDate *date = [NSDate dateWithTimeIntervalSinceReferenceDate:timeInSeconds]];
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:#"hh:mm:ss a"];
NSString *dateString = [formatter stringFromDate:date];

Assuming you want the time formatted according to the current locale on a "normal" day (i.e. one without time zone changes),
double seconds = 54000;
NSDateFormatter * fmt = [NSDateFormatter new];
[dateFormatter setDateStyle:NSDateFormatterNoStyle];
[dateFormatter setTimeStyle:NSDateFormatterLongStyle];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSDate *date = [NSDate dateWithTimeIntervalSinceReferenceDate:seconds];
NSString * s = [fmt stringFromDate:date];
// If you are not using ARC,
[fmt release];

Related

Convert NSString to NSDate in objective c [duplicate]

This question already has answers here:
Converting NSString to NSDate (and back again)
(17 answers)
Closed 5 years ago.
I am getting a NSString as #"27-Jan-2018 00:00".i just want to convert this string to NSDate class.Kindly suggest some methods to achieve this.Thanks in advance!
Try this...
NSString *dateString = #"27-Jan-2018 00:00";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[NSLocale localeWithLocaleIdentifier:#"en_US_POSIX"]];
[dateFormatter setDateFormat:#"dd-MMM-yyyy HH:mm"];
NSDate *dateFromString = [dateFormatter dateFromString:dateString];
NSLog(#"%#",dateFromString);
Please, check it below code. Maybe its work for you and your needs.
-(void)DateChange
{
NSString *Input_Date =#"31-Jan-18 04:25 AM";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd-MMM-yy hh:mm a"];
NSDate * Format_date = [dateFormatter dateFromString:Input_Date];
[dateFormatter setDateFormat:#"dd-MMM-yy hh:mm a"];
NSString *Change_date = [dateFormatter stringFromDate:Format_date];
NSLog(#"Final Change Date :- %#",Change_date);
}
My Output is :-
Final Change Date :- 31-Jan-18 04:25 AM

Converting Date to proper format when the Value obtained from the Db is in format 2012-07-13 00:00:00.0 [duplicate]

This question already has answers here:
Formatting NSDate into particular styles for both year, month, day, and hour, minute, seconds
(8 answers)
Closed 9 years ago.
I know there are lot of questions on this topic . But I am not getting a proper solution to my
problem.I am retrieving Date from db , I am getting the Output as
as 2012-07-13 00:00:00.0 .
I am getting the output in the string format . I want the result of type string in the
format MM/dd/yyyy.I am doing the following
NSString *dateStr=#"2012-07-13 00:00:00.0";
NSDateFormatter *df=[NSDateFormatter new];
[df setDateFormat:#"yyyy-MM-dd"];
NSDate *res=[df dateFromString:dateStr];
But here I am getting res as nil .
You need to tell the formatter the whole format of the date, including hours, minutes...
This works:
NSString *dateStr=#"2012-07-13 00:00:00.0";
NSDateFormatter *df=[NSDateFormatter new];
[df setDateFormat:#"yyyy-MM-dd hh:mm:ss.S"];
NSDate *res=[df dateFromString:dateStr];
[df setDateFormat:#"MM/dd/yyyy"];
NSString *myString =[df stringFromDate:res];
Use this one
NSString *dateStr=#"2012-07-13 00:00:00.0";
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"yyyy-MM-dd HH:mm:ss.S"];
NSDate *date = [df dateFromString: dateStr];
[df setDateFormat:#"MM/dd/yyyy"];
NSString *convertedString = [df stringFromDate:date];
NSLog(#"Your String : %#",convertedString);
Output:
Your String : 07/13/2012
try this:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd HH:mm:ss.s"];
NSDate *aDate=[formatter dateFromString:[NSString stringWithFormat:#"%#", #"2012-07-13 00:00:00.0"]];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterShortStyle];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
NSLog(#"%#", [dateFormatter stringFromDate:aDate]);
Try this
NSString *dateStr=#"2012-07-13 00:00:00.0";
NSDateFormatter *dateFormatter=[NSDateFormatter new];
[dateFormatter setDateFormat:#"yyyy-MM-dd hh:mm:ss.S"];
NSDate *dateObj=[dateFormatter dateFromString:dateStr];
[dateFormatter setDateFormat:#"MM/dd/yyyy"];
NSString *myString = [dateFormatter stringFromDate:dateObj];
NSLog(#"%#",myString);

Time from a TextView into NSDate

I'm building an iPhone app to take times: a car has to get at a given time at a point, so if the car is sooner or later, gets points.
I need to obtain the most precise timing as I can, so I want to use milliseconds, but as far results are disappointing.
The time is typed on a TextField, let's say 12:17:22 and the code is the following:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
[dateFormatter setDateFormat:#"HH:mm:ss"];
timeLegStart = [NSDate new];
timeLegStart = [dateFormatter dateFromString:setStartTime.text];
NSLog(#"timeLegStart = %#",timeLegStart);
The output is 1970-01-01 11:17:22 +0000, and I'd like it to be 12:17:22:000
Set your NSDateFormatter's properties like this (replacing with your time zone info):
NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:#"pt_BR"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"America/Sao_Paulo"]];
[dateFormatter setDateFormat:#"HH':'mm':'ss' 'z"];
[dateFormatter setLocale:locale];
The problem here is that NSDate comprises of date and time and they are not separately available. When you are giving the input date with time specs only it load the calendar start date values with the time you provided and creates a date. Hence here comes the date as output.
Actually there is no problem to work with that.
You can use the same formatter to get the time retrieved back:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
[dateFormatter setDateFormat:#"HH:mm:ss"];
NSDate* timeLegStart = [NSDate new];
timeLegStart = [dateFormatter dateFromString:#"12:12:12"];
NSLog(#"timeLegStart = %#",[dateFormatter stringFromDate:timeLegStart]);
It will give you:
timeLegStart = 12:12:12
EDIT : when logging NSDate it will be in GMT format only.Since you set the timezone to local there date will be always displayed like what you got.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
[dateFormatter setDateFormat:#"HH:mm:ss"];
NSDate* timeLegStart = [NSDate new];
timeLegStart = [dateFormatter dateFromString:#"12:12:12"];
NSLog(#"timeLegStart = %#",timeLegStart);
Will give you:
2000-01-01 12:12:12 +0000
Well, I've sorted it out this way:
- (void)fijaTiempoInicio
{
//getting current day
NSDateComponents *dateComponents = [[NSCalendar currentCalendar] components:~ NSTimeZoneCalendarUnit fromDate:[NSDate date]];
[dateComponents setTimeZone:[NSTimeZone timeZoneWithName:#"Europe/Spain"]];
NSDate *currentDate = [[NSCalendar currentCalendar] dateFromComponents:dateComponents];
//extracting year, month and day from current date
NSDateFormatter *df = [[NSDateFormatter alloc]init];
[df setDateFormat:#"dd"];
NSString *currentDayString = [NSString stringWithFormat:#"%#",[df stringFromDate:currentDate]];
[df setDateFormat:#"MM"];
NSString *currentMonthString = [NSString stringWithFormat:#"%#",[df stringFromDate:currentDate]];
[df setDateFormat:#"yyyy"];
NSString *currentYearString = [NSString stringWithFormat:#"%#", [df stringFromDate:currentDate]];
NSDateFormatter *ft = [[NSDateFormatter alloc]init];
[ft setTimeZone:[NSTimeZone localTimeZone]];
[ft setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSString *tempDate = [NSString stringWithFormat:#"%#-%#-%# %#",currentYearString,currentMonthString,currentDayString,txtTiempoInicioTramo.text];
NSLog(#"tempDate= %#",tempDate);
startTime = [ft dateFromString:tempDate];
NSLog(#"startTime= %#",startTime);
Then, anothe method calculate the difference between two NSDates, which is a figure in milliseconds -a float
- (IBAction)btnCapturarTiempo:(id)sender
{
NSDateFormatter *formatCarTime = [[NSDateFormatter alloc]init];
[formatCarTime setDateFormat:#"HH:mm:ss"];
[formatCarTime setTimeZone:[NSTimeZone timeZoneWithName:#"Europe/Madrid"]];
carTime = [NSDate date];
difference = [carTime timeIntervalSinceDate:startTime];
labelTiempoCoche.text = [NSString stringWithFormat:#"%f",difference];

Calculating actual day's midnight date [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Getting date from [NSDate date] off by a few hours
I want to calculate the actual day's midnight time using the actual day's date here is the code:
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:#"dd/MM/yyyy"];
NSDate *today = [NSDate date];
NSString *todayStr = [NSString stringWithFormat:#"%# 12:59 PM",[formatter stringFromDate:today]];
NSLog(#"%#",todayStr);
[formatter setDateFormat:#"dd/MM/yyyy hh:mm a"];
NSDate *midNight = [formatter dateFromString:todayStr];
NSLog(#"%#",midNight);
But the log I get is :
23/01/2013 12:59 PM
2013-01-23 10:59:00 +0000
Why does midNight isn't the actual midnight and why does it have a different format from formatter?
The midnight you calculated with date formatter takes the localization into account.
So, you are living in a UTC+2 timezone.
You can add a 'Z' for UTC.
See more at http://www.w3.org/TR/NOTE-datetime
This calculates midnight for your local timezone.
NSDate *d = [NSDate date];
NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *comps = [cal components:NSEraCalendarUnit|NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:d];
d = [cal dateFromComponents:comps];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"dd/MM/yyyy hh:mm a"];
NSString *dateStr = [formatter stringFromDate:d];
NSLog(#"%#", dateStr);
prints 23/01/2013 12:00 AM

how to display time and date in a label of selected zone xcode

Can any 1 help me to get this problem done I'm trying to display date and time of selected time zone n a label or text field I tried below code but its Displaying current time of system for all selected zone :( thanks in advance
NSDate *now = [NSDate date];
NSCalendar *gregorian=[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
[gregorian setTimeZone:[NSTimeZone timeZoneWithName:selectedzone]];
NSDateComponents* timeZoneComps = [gregorian components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit|NSTimeZoneCalendarUnit fromDate:now];
NSDate *selectedDate=[gregorian dateFromComponents:timeZoneComps];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterFullStyle];
[formatter setDateFormat:#"dd-MM-yyyy HH:mm:ss "];
NSString *strSelectedDate= [formatter stringFromDate:selectedDate];
zone.text = strSelectedDate;
[NSDate date]returns a date object representing the current date and time, no matter where you are. NSDates are not subject to places or time zones. There is just one NSDate that represents now or any other moment for that matter, not different date objects for every time timezone. Therefore, you should not attempt to convert a date between time zones.
NSDate *now = [NSDate date];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterShortStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setTimeZone:[NSTimeZone timeZoneWithName:#"Australia/Sydney"]];
NSLog(#"%#",[formatter stringFromDate:now]); //--> 9/9/11 11:54 PM
[formatter setTimeZone:[NSTimeZone timeZoneWithName:#"Europe/Paris"]];
NSLog(#"%#",[formatter stringFromDate:now]);