How to change format of date/time? - iphone

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.

Related

Alfresco: changing date and time formats

I have a few places where I need to change how the date format displays in my Alfresco share:
Calendar Format
Needs to be in MM/DD/YYYY format.
Date in Info.ftl Control
Needs to be in DDD MM DD YYYY format.
Date().toString()
This is being displayed in a data list. Ideally it should read: "Tue Jul 2015 8:27:51 (EST)"
I located common.properties and made the following changes:
## Date Formats
#Used client side (uses Alfresco.util.formatDate)
date-format.default=mmm ddd d yyyy HH:MM:ss
date-format.defaultDateOnly=mmm ddd d yyyy
date-format.shortDate=m/d/yy
date-format.mediumDate=mmm d, yyyy
date-format.mediumDateNoYear=mmm d
date-format.longDate=mmmm dd, yyyy
date-format.longDateNoYear=mmmm dd
date-format.fullDate=mmmm, d dddd, yyyy
date-format.fullDateTime=mmmm, d dddd, yyyy 'at' h:MM TT
date-format.shortTime=h:MM TT
date-format.mediumTime=h:MM:ss TT
date-format.longTime=h:MM:ss TT Z
date-format.monthYear=mmmm yyyy
date-format.dayDateMonth=mmmm, d dddd
date-format.am=am
date-format.pm=pm
But none of them seem to affect any date formats anywhere. My questions:
Where do I need to make additional changes to get the above formats to work?
How do I change the time zone?
Essentially, I need to change everything to Eastern US time zone and formats.
Different files where date format are mentioned. Each of them are responsible for rendering dates in different places
<ALF_HOME>\tomcat\webapps\share\WEB-INF\classes\alfresco\site-webscripts\org\alfresco\components\form\form.get_en.properties
<ALF_HOME>\tomcat\webapps\share\WEB-INF\classes\alfresco\messages\common.properties
<ALF_HOME>\tomcat\webapps\share\WEB-INF\classes\alfresco\messages
common.properties\slingshot.properties
We need to edit the \tomcat\webapps\share\WEB-INF\classes\alfresco\messages\common.properties as well as \tomcat\webapps\share\WEB-INF\classes\alfresco\messages\common_en.properties the second file the locale specific file should also be modified.
Modify the below key's value
date-format.default=mm/dd/yyyy
These changes only impact the display format of date, if we need to make changes for form we need to modify in \tomcat\shared\classes\alfresco\web-extension\site-webscripts\org\alfresco\components\form**form.get_en.properties** and form.get.properties.
Hope this will help you :)

Displaying Date and Time in Codesys

I am trying to display system time in Codesys Visualization.
I am using "#dt" to get the time and displaying it in Text field using
%t[ddd MMM dd.MM.yyyy HH:mm:ss] place holder.
I want to get display the local time. But I am getting in 'UTC' time stander.
What I should do to get the local time?
I am not sure if the #dt is really necessary. I just typed the expression %t[ddd MMM dd.MM.yyyy HH:mm:ss] in the Text field of a rectangle object and it displays your PC's local time.
CodeSYS2 %t%a %b %d.%m.%y %H:%M:%S

convert time to 12hr format

Please tell me how to convert time to 12 hr format..
I have a string which i have converted to date but it is in 24 hr format....
You could use the 24 hour clock format instead of the 12 hour format. So use a NSDateFormatter with H in the format string. Using one of the predefined formats will end up using your systems localization which probably is the 12 hour format.
NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
[inputFormatter setDateFormat:#"HH:mm"];
another solution is presented here using regex:
24hr to 12hr time convert, insert character to string
considering you have an hour int named time...
if (time>12)
time=time-12;
When you say you've converted it to a date, what do you mean? Do you mean you have it in an NSDate object? If so, the date is not in 24hr date format. NSDate does not concern itself with how the date will look on-screen.
The formatting comes from your localisation settings. Which is how it should be. I don't want times in 12hr format any more than you want them in 24hr.
But to directly answer the question, you may have to write your own formatting code if you want to override the defaults. I had to do this to work around bugs in NSDateFormatter when I received dates from a third party data source.
To change the date format from 12 to 24 hours or from 24 to 12 simply change hh:mm to HH:mm format where h is for 12 hours and H is for 24 hours.
Remember to change your location.
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"HH:mm"];

how to change the date format in the objective-c

after selecting the date from the date picker, i am getting the date in the format in view like yy/mm/dd .
i want to the date format in my view like mm/dd/yy.
how can i do this.
You can use the following snippet to convert the date to the format you desire. Note that month use uppercase 'MM' in the format:
[dateFormatter setDateFormat:#"MM/dd/yy"];
dateString = [dateFormatter stringFromDate:theDate];
For a full list of the formats available, have a look at the Unicode Standard. With a few minor exceptions, these formats are all supported on the iPhone.

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.