How to get the date with specific hour and minute of today from "HH:mm" string in Objecive-C - objective-c

I have a time string like "HH:mm" format and I want to get a NSDate object of today at that time.
I am using NSDateFormatter but it not work.
Here is my code:
NSString *dateString = #"21:00";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"HH:mm"];
NSDate *date = [dateFormatter dateFromString:dateString];
// date: __NSTaggedDate * 2000-01-01 07:00:00 UTC 0xfa629653dac0e51b
I want the date to be 2022-10-27 21:00:00;

The defaultDate property on NSDateFormatter does what you want :
NSString *dateString = #"21:00";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"HH:mm"];
dateFormatter.defaultDate = [NSDate date];
NSDate *date = [dateFormatter dateFromString:dateString];
// 2022-10-27 20:00:26 UTC

Related

Objective C - Convert DateTime to Local DateTime

I am pretty new to Objective C and it has been horrible experience to get the current device datetime in systemTimeZone. This is what I have:
NSDate *now = [NSDate date];
NSDateFormatter *DateFormatter=[[NSDateFormatter alloc] init];
[DateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
[DateFormatter setDateFormat:#"MM-dd-yyyy HH:mm"];
NSString *currentDateTime = [DateFormatter stringFromDate:now];
NSDate *curDate = [DateFormatter dateFromString:currentDateTime];
Line 5 NSString has the correct local datetime in currentDateTime string variable. But Line 6 again switches back to UTC DateTime. I do not understand why it would switch back to UTC even though the DateFormatter has the systemTimezone set. Can you please help me find out what is that I am missing?
Just set the timeZone in the dateFormatter, This code is enough
NSString *dateString = #"24 08 2011 09:45PM";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd MM yyyy hh:mma"];
NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:#"BST"];
[dateFormatter setTimeZone:sourceTimeZone];
NSDate *dateFromString = [dateFormatter dateFromString:dateString];
The dateFromString will now have the date 24 08 2011 08:45PM(GMT).. Then to convert this to string with local time just code the following,
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[formatter setTimeZone:[NSTimeZone localTimeZone]];
[formatter setLocale:[NSLocale systemLocale]];
[dateFormatter setDateFormat:#"dd MM yyyy hh:mma"];
NSString *stringFromDAte = [dateFormatter stringFromDate:dateString];
or You can also try this one:
NSTimeInterval seconds; // assume this exists
NSDate* ts_utc = [NSDate dateWithTimeIntervalSince1970:seconds];
NSDateFormatter* df_utc = [[[NSDateFormatter alloc] init] autorelease];
[df_utc setTimeZone:[NSTimeZone timeZoneWithName:#"UTC"]];
[df_utc setDateFormat:#"yyyy.MM.dd G 'at' HH:mm:ss zzz"];
NSDateFormatter* df_local = [[[NSDateFormatter alloc] init] autorelease];
[df_local setTimeZone:[NSTimeZone timeZoneWithName:#"EST"]];
[df_local setDateFormat:#"yyyy.MM.dd G 'at' HH:mm:ss zzz"];
NSString* ts_utc_string = [df_utc stringFromDate:ts_utc];
NSString* ts_local_string = [df_local stringFromDate:ts_utc];
// you can also use NSDateFormatter dateFromString to go the opposite way
Table of formatting string parameters:
https://waracle.com/iphone-nsdateformatter-date-formatting-table/
If performance is a priority, you may want to consider using strftime
https://developer.apple.com/legacy/library/documentation/Darwin/Reference/ManPages/man3/strftime.3.html

NSDate returning nil for the NSDateFormatter set correctly

I am getting the date in NSString format as mentioned below.
Input:datestring = 30/04/2013 04:49 PM
I am applying the format for NSDateformatter as mentioned below to convert this NSString to NSDate .
Format:dateFormat= dd/MM/yyyy hh:mm a
I still get the date conversion to Nil. Please help as I am not understanding this issue.Here is my code snippet to explain what i am doing in the utility function.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:dateFormat];
NSDate *date = [dateFormatter dateFromString:dateString];
return date;
Try This....
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd/MM/yyyy hh:mm a"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"UTC"]];
NSDate *date = [dateFormatter dateFromString:#"30/04/2013 04:49 PM"];
NSLog(#"%#",date);

NSDateFormatter stringFromDate not returning a string

I'm trying to change a saved date to mountain standard time using NSDateFormatter but it doesn't seem to be working.
it's outputting
"currentDate: 2013-02-22 23:20:20 +0000 date With date formatter: other 2000-01-01 07:00:00 +0000"
to the console.
It looks like the string isn't being created correctly, but I seem to be calling it in the same way I've seen it called normally.
Suggestions?
NSTimeZone * mtnTimeZ= [NSTimeZone timeZoneWithAbbreviation:#"MST"];
NSDate *currentDate = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setTimeZone:mtnTimeZ];
NSString * timeZ = [dateFormatter stringFromDate:currentDate];
NSDate * newDate = [dateFormatter dateFromString:timeZ];
NSLog(#"currentDate: %# string With dateFormatter: %# date made from string %#", currentDate, timeZ, newDate);
You need to set the style for specify date and time formats in order of it to work:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterFullStyle];
[dateFormatter setTimeStyle:NSDateFormatterFullStyle];
[dateFormatter setTimeZone:[NSTimeZone defaultTimeZone]];
NSString * dateCurrentTZ = [dateFormatter stringFromDate:[NSDate date]];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"MST"]];
NSString * dateMSTZ = [dateFormatter stringFromDate:[NSDate date]];
NSLog(#"Date In Current Time Zone: %#", dateCurrentTZ);
NSLog(#"Date In MST: %#", dateMSTZ);
In case you want to specify your own format:
NSDateFormatter Documentation
Then look for "Fixed Formats" based on iOS & Mac OSX ver. you are targeting.
Try putting the following line:
[dateFormatter setDateFormat:#"yyyy-MM-dd hh:mm:ss"];
like this:
NSTimeZone * mtnTimeZ= [NSTimeZone timeZoneWithAbbreviation:#"MST"];
NSDate *currentDate = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:#"yyyy-MM-dd hh:mm:ss"];
[dateFormatter setTimeZone:mtnTimeZ];
NSString * timeZ = [dateFormatter stringFromDate:currentDate];
NSDate * newDate = [dateFormatter dateFromString:timeZ];
NSLog(#"currentDate: %# string With dateFormatter: %# date made from string %#", currentDate, timeZ, newDate);

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];

NSString to NSDate returns wrong date

I try to convert my NSString to NSDate object, but NSDateFormatter returns me a strange value.
Here is code:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd HH:mm"];
NSDate *date = [dateFormat dateFromString:#"2012-08-15 00:00"];
[dateFormat release];
date value is 2012-08-14 21:00 +0000. It is 3 hours difference between NSString value and NSDate value. I think I've missed something, but I don't know what.
This is what i use:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd HH:mm:ss ZZZ"];
NSDate *date = [dateFormat dateFromString:#"2012-08-15 00:00:00 +0000"];
NSLog(#"\n\n DATE: %# \n\n\n", date);
The +0000 is timezone, so make sure you use your timezone, like +0400.
Edit:
If you can't change the string, you can use this code to do it:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd HH:mm"];
[dateFormat setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSDate *date = [dateFormat dateFromString:#"2012-08-15 00:00"];
As i knew NSDate holds Grinwich time, so if you are in Moscow time zone, everything is wright
In objective c for NSDate if you did not set the setTimeZone, NSDate will take default timezone as localTimeZone. so if you need to get the exact date which you give as NSString string format, you need to setTimeZone as UTC. Follow the sample code, I guess it will be helpful for you.
NSDateFormatter *loacalformatter=[[NSDateFormatter alloc]init];
[loacalformatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSDate *localDate =[loacalformatter dateFromString:#"2012-08-15 00:00"];
NSLog(#"localDate :%#",localDate);
NSDateFormatter *UTCformatter=[[NSDateFormatter alloc]init];
[UTCformatter setTimeZone:[NSTimeZone timeZoneWithName:#"UTC"]];
[UTCformatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSDate *UTCDate =[UTCformatter dateFromString:#"2012-08-15 00:00"];
NSLog(#"UTCDate :%#",UTCDate);
UTCDate :2012-08-15 00:00 +0000 (GMT+00:00)
As suggested in the comments, if the date you receive is UTC then you need to convert it to your local timezone. Apple recommend you always use a properly configured NSDateFormatter when displaying dates, to handle localisation issues.
Here's some example code for turning an NSDate into an NSString:
NSDate *date = // initialised elsewhere
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
dateFormat.locale = [NSLocale currentLocale];
dateFormat.timeZone = [NSTimeZone localTimeZone];
dateFormat.timeStyle = NSDateFormatterShortStyle;
dateFormat.dateStyle = NSDateFormatterShortStyle;
dateFormat.locale = [NSLocale currentLocale];
NSString *dateAsString = [dateFormat stringFromDate:date];