Can I get locale format for apex:outputText? - salesforce

I know that you can format a date like this:
<apex:outputText value="{0,date,MM'/'dd'/'yyyy}">
<apex:param value="{!contact.Birthdate}" />
</apex:outputText>
However, my application will be distributed to multiple countries. On some (such as mine), date format is dd/MM/yyyy, but on others (such as in the US) the format is MM/dd/yyyy.
Is there a way I can tell Visualforce to use the locale's short date format?

The only solution to this issue that is actually working correctly was originally posted on http://mindfiresfdcprofessionals.wordpress.com/2013/10/25/how-to-dispaly-locale-formatted-datetime-in-visual-force/. I'm posting below code just in case the link will suddenly disappear:
You have to create new VF component called LocaleDate:
<apex:component access="global" controller="LocaleDateController">
<apex:attribute assignTo="{!dateValue}" description="The Date value to be rendered based upon the user's
locale" name="date_Value" type="Date"></apex:attribute>
{!TimeZoneValue}
</apex:component>
Its controller:
public with sharing class LocaleDateController {
public Date dateValue{get;set;}
public String getTimeZoneValue() {
if( dateValue != null ) {
String localeFormatT = dateValue.format();
return localeFormatT;
}
return null;
}
}
And then use such component like that:
<c:LocaleDate date_Value="{!TODAY()}"/>
It is working on any date value you enter, not necessarily sObject field. The whole idea is based on fact, that Date.format() function is creating String based on locale of current user. You can create in the same way wrapper controllers for DateTime and Decimal datatype.

Apex param based formating is based on Java's MessageFormat:
<apex:outputText value="{0, date, short}">
<apex:param value="{!contact.Birthdate}" />
</apex:outputText>

Assuming you are assigning the variable of type date in the value attribute, it should automatically handle the locale for you. So you don't need to format it explicitly.

Related

How format datetime for sql server in laravel?

im trying to format datetime values in laravel to make them match with sql server format, im getting this error:
The separation symbol could not be found Unexpected data found.
Unexpected data found. Trailing data
Im using a custom format in my model:
protected $dateFormat = 'd-m-Y H:i:s';
It works good for the create method con controllers, there is no problem, the error happen when try to update and I think is related to how the value is stored in the database.
Using tinker I retrieve the value of updated_at in my model and is shown in the next format:
updated_at: "2018-07-24 09:14:09.000"
So when Im updating this value the format used is:
protected $dateFormat = 'd-m-Y H:i:s';
I think it should be something like d-m-Y H:i:s.000 but doesnt work, how can I solve this?
I had exactly the same issue, when switching to laravel version 5.6. Now I found the correct way to handle this.
Create a Base class like this and add a public method getDateFormat.
There you return the date a little bit different
return 'Y-d-m H:i:s.v';
See that date (d) and month (m) are switched and at the end you need to put a .v which stands for milliseconds. This worked for me fine. See here my complete Base class:
<?php
namespace App\Models\Base;
use Illuminate\Database\Eloquent\Model;
class BaseModel extends Model
{
public function getDateFormat()
{
return 'Y-d-m H:i:s.v';
}
public $timestamps = false;
}
What version of Laravel are you using? This was fixed in a recent release (can't remember exactly which) that relies on PHP 7.
The dateFormat should be "Y-m-d H:i:s.v". v is Milliseconds (added in PHP 7.0.0). The problem you're experiencing with updates and inserts is caused when the updated/inserted model is returned. It is passed to Carbon to convert date and datetime fields into instances of Carbon, but the datetime fields have 'trailing
You can also try this one in SQL/Select Query:
\DB::raw ("DATE_FORMAT(comments.date,'%d-%m-%Y %H:%i:%s') as sth_date")
E.g:
Comment::orderBy('date','desc')->with('user')
->join('users','comments.id_user', '=', 'users.id')
->select('users.username', 'comments.id', 'comments.id_race', 'comments.status', 'comments.ip_address', 'comments.id_user',
\DB::raw ("DATE_FORMAT(comments.date,'%d-%m-%Y %H:%i:%s') as sth ") );
Time fraction is a configuration of your database, you can see it below, so you can just try to switch on your database.
https://dev.mysql.com/doc/refman/5.6/en/fractional-seconds.html
But using something like $d->format("Y-m-d H:i:s.u") you can format/manipulate using class Datetime().

How to format the date string according to the country in angular?

I had the following code where i get the data from JSON response. I want to change the date string format w.r.t to country locale. I'm using angular-translate for translations and added all the strings in respective locale json file. For example i want to add the date format (dd/mm/year) for es_ES(spanish) locale and (mm/dd/year) for en_US. Can i add the date format by any chance in the Json file or how can i add a filter to format in markup itself? Is it possible at all?
//Sample Html markup
<tr ng-repeat="data in data.list">
<td>{{data.originalDate}}</td>
<td>{{data.expiryDate}}</td>
</tr>
//sampleJsonResponse
{
"data": [
"{originalDate:\"09/30/2017\",expiryDate:\"10/30/2018\"}"
]
}
Thanks
You can use just javascript to transform date to different locale:
first you will need to create date object with (new Date(yourdate)):
let date = new Date('10/30/2018');
then set date to specific locale use dash instead of underscore
date.toLocaleString('es-ES')
for your purposes you can just do:
new Date(data[0].originalDate).toLocaleString('es-ES')
new Date(data[0].expiryDate).toLocaleString('es-ES')
or do a map on entire data like this:
data.map(value => {
return {
originalDate: new Date(value.originalDate).toLocaleString('es-ES'),
expiryDate: new Date(value.expiryDate).toLocaleString('es-ES')
}
});
More info here:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleDateString
For all other advanced date manipulation I would suggest momentJS: https://momentjs.com/
I'm using sugar.js javascript library for date formatting and also it has other general functions such as number and string formatting which serves well in my code base.

Package: Composite.News

How do I change date format settings for the Composite.News Package? (or perhaps the whole Composite C1 Site)
I would like to use the date format YYYY/M/D for all news instead of the standard M/D/YYYY
I´ve been looking in the Razor Functions (NewsList & NewsLatest) but cant find any date format related settings.
There is no setting like this in the package out-of-the-box.
You can however change that. In the Razor function "Composite.News.NewsList" find the helper method "Date" (currently at the end of the file).
#helper Date(DateTime date)
{
<span class="text-muted">
<span class="icon-calendar"></span>
#date.ToShortDateString()
</span>
}
Replace:
#date.ToShortDateString()
with:
#date.ToString("yyyy/M/d")
or follow the guide on other format specifiers here https://msdn.microsoft.com/en-us/library/8kb3ddd4%28v=vs.110%29.aspx
Of course, this will be hard-coded. So you can add another parameter to the Razor function and use its value instead:
[FunctionParameter(Label = "Date format", Help = "...", DefaultValue = "yyyy/M/d")]
public string DateFormat { get; set; }
...
#helper Date(DateTime date)
{
<span class="text-muted">
<span class="icon-calendar"></span>
#date.ToString(DateFormat)
</span>
}
Thus you can use other format specifiers via the function properties in the GUI.

Posting date values in REST service will not create date fields in the Domino back end

I'm creating an AngularJS HTML app using Domino in the back-end. The communication is 100% rest-based via DDS
When I send date values they don't get converted to date items on the Domino document. The values are always stored as strings
I have tried various formats on the date string with no luck
Does anyone know is this is even possible with the Domino Data Services ?
I'm using Angulars $http service with the PATCH method to update changed values only
It is possible to store/ update a data in a document using Domino Data Services.
To get it to work you need to send the date as a string in ISO 8601 Extended format. That's the format that the toISOString() function returns in JavaScript for a Date object. On the form that you're trying to create or update, you'll need to have that field added as a Date/Time field. Adding the computewithform parameter to the request isn't required.
Here's a sample JSON object that, when send as a POST or PATCH request to DDS, will create/ update the LastVisit field as a DateTime field (assuming that field is on the form).
{
"FirstName":"Barney",
"LastName":"Bloomberg",
"LastVisit" : "2013-12-21T12:18:18Z"
}
The field name in the json string must be EXACTLY as on the form.
I had the similar problem where I had a field called 'TTL' on the form but my json generated by API using a class the field was named 'ttl'.
This resulted in a String as value for the date field, not a date.
This works :-)
I have extended the sample that I used in my presentation in the following way:
Added a field "WakeupTime" on the form. Set it to Date/Time, and select to display date and time. The sample output is 08-01-2016 16:11:42.
So reading the sample data using this url:
.../json.nsf/api/data/documents/unid/33735D0BCE799E01C1257CC3007A7221
I get something like this back:
{
"#href": "/demo/json.nsf/api/data/documents/unid/33735D0BCE799E01C1257CC3007A7221",
"#unid": "33735D0BCE799E01C1257CC3007A7221",
"#noteid": "902",
"#created": "2014-04-23T22:17:26Z",
"#modified": "2016-01-08T15:09:57Z",
"#authors": [
"Anonymous",
"CN=John Dalsgaard/O=D-D"
],
"#form": "Person",
"Unid": "33735D0BCE799E01C1257CC3007A7221",
"Key": "33735D0BCE799E01C1257CC3007A7221",
"Name": "Peter Hansen",
"Email": "ph#mail.dk",
"YearBorn": 1955,
"WakeupTime": "2016-08-01T05:33:10Z"
}
Important! - this gives me the exact format that I need to use for the WakeupTime field!
So if I then post a PATCH back with select fields:
{
"Email":"peter.hansen#mail.dk",
"YearBorn":1953,
"WakeupTime":"2016-01-08T05:33:40Z"
}
... and re-read the data then the fields are updated. And if I check in the Notes client I can see that the field is a date/time field :-)
Same happens if I create a new entry/document - the field is still the right type.
You have to be very aware of how you handle timezones though! The data are transferred as GMT :-)

Visualforce: How do I print the Current Date?

How do I print the current date on Visualforce?
Stuff like Today(), System.now, doesn't work.
Haven't been able to find any documentation or stackoverflow questions regarding this subject.
Visualforce has Data and Time functions too, you can use {!NOW()} or {!TODAY()}.
For more details go here
In your controller, create a property:
public Date Today { get { return Date.today(); }}
In the VF page, try using:
<apex:outputText value="{0,date}">
<apex:param value="{!Today}" />
</apex:outputText>
Details on formatting the date can be found here and here.

Resources