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

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.

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

Converting string date to Human Readable Format HRF (Stata)

version: stata15
Date (format %9s), type 8s - 20171230
date
20171230
20171230
I want to be able to see 30dec2017,
gen newdate = date(date, "YMD")
format newdate %td
list
here is what I see
date newdate
20171230 .
Why do I see a missing data in the new date variable? what am I doing wrong in the newdate %td command ?
ANSWER TO ORIGINAL QUESTION:
This makes no obvious sense to me. Questions like this waste your time as well.
You don't give a clear reproducible example of your data. In a recent version of Stata, help dataex tells you how to do that. If you're using an older version of Stata then you should be telling us that.
Your first sentence seems to be telling us that you have a string variable date with storage type str8 and display format %9s.
If that is so, then tostring is pointless, as you already have a string variable.
The statement gen date = date(date, "YMD") is illegal, as date already exists as a variable. If you typed that, it will not have worked. If you typed something else, you should tell us.
The format %td should give you a fairly readable date. So, you've already answered your own question. If you have a numeric date variable, read help datetime display formats to learn more.
EDIT The fact that most of the above is irrelevant to the real question shows how you wasted time not asking the real question.
ANSWER TO MODIFIED QUESTION:
I can't reproduce your problem. You report a missing date result, and clearly no kind of display format will make sense of that. But Stata 15.1 does this for me. So, something is wrong with what you are telling us. Perhaps your Stata is corrupted, or something else is not as you say.
clear
set obs 1
gen str8 date = "20171230"
gen newdate = daily(date, "YMD")
list
+--------------------+
| date newdate |
|--------------------|
1. | 20171230 21183 |
+--------------------+
21183 will show nicely with a %td format. Using daily() rather than date() is irrelevant, as the same result is yielded by date().

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

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.

Bug in Zend_Date (back in time)

I have a very strange problem, Zend_Date is converting my timestamp to a year earlier.
In my action:
// Timestamp
$intTime = 1293922800;
// Zend_Date object
$objZendDate = new Zend_Date($intTime);
// Get date
echo date('Y-m-d',$intTime).'<br>';
echo $objZendDate->get('YYYY-MM-dd');
This outputs:
2011-01-02
2010-01-02
Can anyone tell me what i'm doing wrong?
From the ZF issue tracker it seems this is a known issue:
Recently a lot of ZF users are filing a bug that Zend_Date returns the wrong year, 2009 instead of 2008. This is however expected behaviour, and NOT A BUG!
From the FAQ:
When using own formats in your code you could come to a situation where you get for example 29.12.2009, but you expected to get 29.12.2008.
There is one year difference: 2009 instead of 2008. You should use the lower cased year constant. See this example:
$date->toString('dd.MM.yyyy');
instead of
$date->toString('dd.MM.YYYY');
From the manual
Note that the default ISO format differs from PHP's format which can be irritating if you have not used in previous. Especially the format specifiers for Year and Minute are often not used in the intended way.
For year there are two specifiers available which are often mistaken. The Y specifier for the ISO year and the y specifier for the real year. The difference is small but significant. Y calculates the ISO year, which is often used for calendar formats. See for example the 31. December 2007. The real year is 2007, but it is the first day of the first week in the week 1 of the year 2008. So, if you are using 'dd.MM.yyyy' you will get '31.December.2007' but if you use 'dd.MM.YYYY' you will get '31.December.2008'. As you see this is no bug but a expected behaviour depending on the used specifiers.
For minute the difference is not so big. ISO uses the specifier m for the minute, unlike PHP which uses i. So if you are getting no minute in your format check if you have used the right specifier.
To add to zwip's answer, what happens behind the scenes is that your date format YYYY-MM-dd is actually translated into o\-m\-d, which is then passed to PHP's date() function internally with the timestamp you provided.
Like mentioned in the other answer, and in the documentation for the o format on the date format page, the calculation of the year based on the ISO week can sometimes result in the year being one different to the value that you expect.

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.