This question already has answers here:
Constructing an NSDate from today's date and a string with the time
(2 answers)
Closed 6 years ago.
I'm parsing a time using NSDateFormatter. How do I move the NSDate object to the current day so that it will be 12:34pm with today's date?
NSDateFormatter *formatter = [NSDateFormatter new];
[formatter setDateFormat:#"h:m a"];
NSDate *startDate = [formatter dateFromString:#"12:34 pm"];
You could get today's date/time from NSDate, then use NSDateComponents to break up both that and your startDate, copy the time components from one to the other, then translate back into an NSDate.
HTH
Related
This question already has answers here:
How to parse a date string into an NSDate object in iOS?
(7 answers)
Closed 3 years ago.
#"2019-12-27T06:42:35Z" app crash when stringDate convert into date
NSDateFormatter*dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd HH:mm:ss Z"];
NSDate*date = [dateFormat dateFromString:sentDate];
return date;
The date format you are providing to your date formatter is wrong. So as per your input, your date format should be yyyy-MM-dd'T'HH:mm:ssZ
This question already has answers here:
NSDate Format outputting wrong date
(5 answers)
Closed 7 years ago.
Important update: Of you hover over a NSDATE with your mouse the degugger will convert the NSDate to your local timezone you have set on you Mac, but if you do a NSLOG you will notice that the NSDate is using the timezone that you assigned to the its respective formatter.
If you want to see in the xcode debugger what the NSDate is for the timezone you are working with go to your date/time settings for you Mac OS and change the Timezone to the one you are testing.
I require a NSDate to be created from the date I pass in, but currently it is set to the the day before I pass in:
NSString *dateStr = #"2015-08-09";
NSDateFormatter *myformatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd"];
NSDate *date = [formatter dateFromString:dateStr];
The code above returns: an NSDate set to '2015-08-08 12:00:00 +0000'
I need an NSDate object set to the datStr I pass in.
This perhaps, from this:
Convert NSDate to NSString with NSDateFormatter with TimeZone without GMT Time Modifier
leave the 'z' lowercase for named timezone, i.e. PST and uppercase 'Z' for -0800
NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setDateFormat:#"MMMM dd, yyyy (EEEE) HH:mm:ss z Z"];
NSDate *now = [NSDate date];
NSString *nsstr = [format stringFromDate:now];
Also, you date should be more robust, like if you want to pass in the current day that your are passing in and it's one day off, then just add a day. The problem, it seems is that the date is returning the correct date which is the end of the last day, add 1 second or a millisecond and it'll probably be corrected, or just hack attack this and add 1 day to the days you are passing in. Be smart! Sometimes you just have to add 1 day to fix stuff and move on.
This question already has an answer here:
returns a date an hour in the future
(1 answer)
Closed 8 years ago.
I will appreciate if someone could please advise me what is wrong with the date formatting. I have the following date as string from webservice :
20141211200300 //yyyyMMddHHmmss -->2014/12/11 20:03:00
But when I format it using the following format it adds 2 hrs
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyyMMddHHmmss"];
NSDate *date = [dateFormatter dateFromString:localDate];
The output I get in console is
2014-12-11 10:03:00 +0000
I tried adding GMT timezone as well as Australia/Brisbane still get the same issue.
You did not show the code that printed the number. If you used NSLog() there is probably a time zone issue, Note that the printed time is in GMT. Is your time zone two hours from GMT?
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Getting date from [NSDate date] off by a few hours
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"M-d-yyyy H:mm"];
NSDate *start= [dateFormatter dateFromString:#"10-24-2012 12:15"];
NSDate *end = [dateFormatter dateFromString:#"10-24-2012 15:30"];
When I print out
NSLog(#"------main_event start %#", start);
NSLog(#"-----main_event end %#", end);
The result is
---main_event start 2012-10-24 19:15:00 +0000
---main_event end 2012-10-24 22:30:00 +0000
Now, it looks like the time added 7 hours automatically, 12:15 becomes 19:15, and 15:30 becomes 22:30.
Why?
because the timezone, where your device is located, is UTC-7.
The output is in UTC (hence the +0000), as a single NSDate will always print out it's time in UTC.
If you use an NSDateFormatter to output the date, it will take your locale in account. See my answer here: NSDate date method returns wrong result
These are correct results. When you use NSLog to output an NSDate object, it displays in GMT. The parsing was done in your local timezone. NSDate objects are alway in GMT. If you want to print the NSDate object in your local timezone then you need an NSDateFormatter to print the date.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do I get the day of the week in Objective-C?
How do I print the day of the week? What object should I use? I need the day (e.g. Sunday, Thursday), not the date in the month.
Update:
Specifically: how can I find the number of the day of the week (e.g. 1, 5)
Make an instance of NSDate (for right now, use [NSDate date]), and then use an NSDateFormatter and a format string as described here.
NSDate *today = [NSDate date];
NSDateFormatter *myFormatter = [[NSDateFormatter alloc] init];
[myFormatter setDateFormat:#"EEEE"]; // day, like "Saturday"
[myFormatter setDateFormat:#"c"]; // day number, like 7 for saturday
NSString *dayOfWeek = [myFormatter stringFromDate:today];
NSLog(#"Today is: %#", dayOfWeek);