Date Formats from Device Based on Locale

Date formats from device based on locale

I thought, this would be just a simple fix. but on seeing some cool answers to this question, went curious about it. Here is my solution.

You can use NSDateFormatter for this, set the style to required formats

NSDateFormatterShortStyle   
NSDateFormatterMediumStyle
NSDateFormatterLongStyle

to know more please take a look at documentation

Below is the snippet for you.

    NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateStyle:NSDateFormatterShortStyle];
NSString *dateformat = [dateFormatter dateFormat]; //M/d/yy
NSString *amtext = [dateFormatter AMSymbol]; //AM
NSString *pmtext = [dateFormatter PMSymbol]; //PM

dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
NSString *timeformat = [dateFormatter dateFormat]; //h:mm a

it will use the user's locale formats. Hope that solves your problem.

Format date string as per device locale settings

The best approach is to not set dateFormat, but rather set dateStyle and timeStyle.

let formatter = DateFormatter()
formatter.timeZone = TimeZone(secondsFromGMT: 0)
formatter.dateStyle = .medium
formatter.timeStyle = .medium
let string = formatter.string(from: Date())

If none of those styles are quite correct, then, go ahead and use dateFormat, but rather than a string literal, set dateFormat using setLocalizedDateFormatFromTemplate(_:).

let formatter = DateFormatter()
formatter.timeZone = TimeZone(secondsFromGMT: 0)
formatter.setLocalizedDateFormatFromTemplate("ddMMyyyy HH:mm")
let string = formatter.string(from: Date())

That displays 09/08/2017 19:42 for US users, 08/09/2017 19:42 for United Kingdom users, and 08.09.2017 19:42 for German users

Date formatting based on user locale on android

You can use the DateFormat class that formats a date according to the user locale.

Example:

String dateOfBirth = "26/02/1974";
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
Date date = null;
try {
date = sdf.parse(dateOfBirth);
} catch (ParseException e) {
// handle exception here !
}
java.text.DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(context);
String s = dateFormat.format(date);

You can use the different methods getLongDateFormat, getMediumDateFormat depending on the level of verbosity you would like to have.

Format time of Date Object depending on device locale

Use DateFormat.getTimeFormat(Context) to get a DateFormat instance, and use that to format the date instead of creating your own format. This will honor the user's locale and 24-hour time preference from device settings.

Documentation: https://developer.android.com/reference/android/text/format/DateFormat.html#getTimeFormat(android.content.Context)

Example:

Date date = new Date();
String text = DateFormat.getTimeFormat(MainActivity.this).format(date);

Use different time formats according to language

I didn't think it would work, but it did. Just put the format you like into the string xml file for me it was:

<string name="date_time_format">dd MMMM yyyy, hh.mm a</string>
<string name="date_time_format">dd.MM.yyyy, HH:mm</string>

Then use it in the SDF like this:

SimpleDateFormat fmtOut = new SimpleDateFormat(context.getString(R.string.date_time_format), Locale.getDefault());

for the "Uhr" at the end of the german format i added a placeholder String that looks like this:

<string name="date_time_string">%s</string>
<string name="date_time_string">%s Uhr</string>

and i return the formated date with the String.format() method:

return String.format(context.getString(R.string.date_time_string), fmtOut.format(date));

Localizing dates in android

    String dateOfBirth = "02/26/1974";
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
Date date = null;
try {
date = sdf.parse(dateOfBirth);
} catch (Exception e) {
// handle exception here !
}
// get localized date formats
Log.i(this,"sdf default: "+new SimpleDateFormat().format(date)); // using my phone locale
DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.DEFAULT, Locale.US);
Log.i(this,"dateFormat US DEFAULT: "+dateFormat.format(date));
dateFormat = DateFormat.getDateInstance(DateFormat.DEFAULT, Locale.GERMAN);
Log.i(this,"dateFormat GERMAN DEFAULT: "+dateFormat.format(date));
dateFormat = DateFormat.getDateInstance(DateFormat.DEFAULT, Locale.CHINESE);
Log.i(this,"dateFormat CHINESE DEFAULT: "+dateFormat.format(date));
dateFormat = DateFormat.getDateInstance(DateFormat.SHORT, Locale.US);
Log.i(this,"dateFormat US SHORT: "+dateFormat.format(date));
dateFormat = DateFormat.getDateInstance(DateFormat.SHORT, Locale.GERMAN);
Log.i(this,"dateFormat GERMAN SHORT: "+dateFormat.format(date));
dateFormat = DateFormat.getDateInstance(DateFormat.SHORT, Locale.CHINESE);
Log.i(this,"dateFormat CHINESE SHORT: "+dateFormat.format(date));

output is:

 sdf default: 26.02.74 0:00
dateFormat US DEFAULT: Feb 26, 1974
dateFormat GERMAN DEFAULT: 26.02.1974
dateFormat CHINESE DEFAULT: 1974-2-26
dateFormat US SHORT: 2/26/74
dateFormat GERMAN SHORT: 26.02.74
dateFormat CHINESE SHORT: 74-2-26

How to get a localized date string in flutter?

You can use second approach by passing locale in DateFormater like below code:

String locale = Localizations.localeOf(context).languageCode;
DateTime now = new DateTime.now();
String dayOfWeek = DateFormat.EEEE(locale).format(now);
String dayMonth = DateFormat.MMMMd(locale).format(now);
String year = DateFormat.y(locale).format(now);

Each named constructor of DateFormat takes a locale as an optional position parameter.

In this case, you don't even need to make your widget stateful.

Getting different time formats on simulator and local devices - Swift iOS

Reason you are getting different result is because the locale of your mac (Simulator) and your iPhone are different and DateFormatter depends on locale to turn date into string. when you don't give DateFormatter explicit locale it uses your device, so if you need same result, set locale.

func today() -> String {
let date = Date()
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .medium
dateFormatter.locale = Locale(identifier: "en_US")
return dateFormatter.string(from: date)
}

here's an interesting site to check different results.
https://nsdateformatter.com



Related Topics



Leave a reply



Submit