obj-c: how to parse a "human friendly" date? - iphone

I've read a many things about parsing date in obj-c, but I can't find anything dealing with dates like "Mon, 25 Apr 2011 11:53 am CDT"... I'd like to convert it to NSDate in a smart way but I'm running out of ideas.
Thanks.

-[NSDateFormatter dateFromString:]
You'd probably use #"EEE, d MMM yyyy hh:mm a zzz" as your date format string.
And by the way, googling "convert string to date objective-c" yields thousands of hits that have correct answers.

NSDateFormatter is a pretty complete class that covers all "human-friendly" date scenarios.
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/Reference/Reference.html
There's also several questions on stackoverflow that seem very similar to your question, so a quick search should help you out.

Related

OpenRefine toDate() conversion fail

I have a sensor log file with dates in the form Mon Nov 30 18:21:40 UTC 2020 that I'd like to convert to OpenRefine dates.
Per GREL Date Functions, I thought the correct transformation would be value.toDate('E M d H:m:s z y'), but I consistently get "Error: Unable to convert to a date".
I've tried simple things like replacing UTC with GMT, without success.
What clue am I missing?
That's a weird date format. I'm not sure why a sensor log wouldn't just use ISO 8601.
Try using value.toDate('EEE MMM d H:m:s Z y').
It's not super obvious from the docs that you need multiple characters, but if you look at the examples at the bottom of this page, you can see them used there.
https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

Can not find a date pattern

Can't find a pattern for this date. Could anyone help me?
I know it's ISO 8601 standart.But everything I have found was without : between 03 and 00.
2018-01-18T08:40:00+03:00
How do I can parse this to Thu Jan 18 00:00:00 GMT+03:00 2018 to this 2018-01-18T08:40:00+03:00?
It looks like this W3C Datetime format is typically used for < lastmod > timestamps in XML.
related & more detailed answer here

Remove Weekday From ToLongDate()

I would like to display a date like...
=Now.ToLongDateString()
BUT without the weekday. So the expression would produce June 21 2011. Not Tuesday, June 21 2011. Is this possible? I don't want the format MM/DD/YY. I would rather have June 21 2011.
I have found an expression that works. This is here for others to use.
=FORMAT(Now,"MMMM dd, yyyy")
I don't like answering my own questions because most here think its a reputation point thing (which its not) SO, If anyone else provides an answer that is more helpful or is "neater", I will toggle off my checkmark and mark yours. Otherwise, I'll leave this up so others can use the answer as per Meta.

How to change format of date/time?

I have this date and time format:
2010-05-19 07:53:30
and would like to change it to:
Wednesday # 7:53PM 5/19/2010
I'm doing this, which gets the current format:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = #"yyyy-MM-dd HH:mm:ss";
but when I change the format, I end up with a null. For example:
formatter.dateFormat = #"hh:mm tt MM-dd-yyyy";
date = [formatter stringFromDate:formattedDate];
date will be null. I want to put the end result into an NSString. It would be nice if time and date could come out as separate properties so I can arrange them however I like. Any ideas on how I can change the formatting?
I think your formatting string is the problem. You should only use the characters you find in the table in UTS#35 Date Format Patterns. I tried your code and while the time hh:mm displays correctly, formatting stops at tt - not in the table!
If you really want characters in the format string that are not in the table you can escape them, like hh 'o''clock' a, zzzz - produces format like "12 o'clock PM, Pacific Daylight Time".
It would be nice if time and date could come out as separate properties so I can arrange them however I like. Any ideas on how I can change the formatting?
You have things backwards. If this is a date/time to be displayed to the user, you need to present it how the user wants it, not how you want it. For instance, most people outside the USA will be confused by MM-dd-yyyy particularly if the day is less than 13. Consider using -setDateStyle: and -setTimeStyle:. That way, the display string will come out as the user expects.

Secure way to figure out if a given date format has an 12h or 24h format?

I know this sucks. Date stuff sucks hard. But: Imagine a date format like "dd-MM-yyyy h:mm" how would you tell for sure what time mode that is? AM / PM or 24 hour? I'd say: If there is no "a" in the date format, then that's no AM / PM stuff and therefore it's nice 24h stuff. What do you think?
If you are given a date, such as 11:15, you can't know whether it is AM or PM. Just as you don't know whether when I say Deer, I mean one or more than one. As a program designer, you have to remove ambiguities or make assumptions. You could either force the data to have AM/PM, or tell the provider of the time to give it to you in 24 hour format, or you can assume that they are smart enough to realize that without AM/PM you have no way of knowing. Not knowing your situation, I can't tell you how to proceed, but there are issues that transcend plain old programming. Like whether 1,000,000,000 is a billion or a milliard or a trillion or whether a ton is 1000 kilograms or ....
You should rather check for a M or m and not an a.
But "dd-MM-yyyy hh:mm" is surely an ambiguous format.
That is, parsing a date that just looks like dd-MM-yyyy hh:mm can't tell you about the 12/24 format.
You could assume it's 24h format, otherwise something is missing or it would look like "dd-MM-yyyy hh:mm X", where X is 'AM' or 'PM'.
The only truly unambiguous format is ISO 8601 'yyyy-MM-dd hh:mm' with 24h times.