Displaying Am and Pm in Lower Case After Date Formatting

Displaying AM and PM in lower case after date formatting

Unfortunately the standard formatting methods don't let you do that. Nor does Joda. I think you're going to have to process your formatted date by a simple post-format replace.

String str = oldstr.replace("AM", "am").replace("PM","pm");

You could use the replaceAll() method that uses regepxs, but I think the above is perhaps sufficient. I'm not doing a blanket toLowerCase() since that could screw up formatting if you change the format string in the future to contain (say) month names or similar.

EDIT: James Jithin's solution looks a lot better, and the proper way to do this (as noted in the comments)

Get AM/PM for a date time in lowercase using only a datetime format

I would personally format it in two parts: the non-am/pm part, and the am/pm part with ToLower:

string formatted = item.PostedOn.ToString("dddd, MMMM d, yyyy a\\t h:mm") +
item.PostedOn.ToString("tt").ToLower();

Another option (which I'll investigate in a sec) is to grab the current DateTimeFormatInfo, create a copy, and set the am/pm designators to the lower case version. Then use that format info for the normal formatting. You'd want to cache the DateTimeFormatInfo, obviously...

EDIT: Despite my comment, I've written the caching bit anyway. It probably won't be faster than the code above (as it involves a lock and a dictionary lookup) but it does make the calling code simpler:

string formatted = item.PostedOn.ToString("dddd, MMMM d, yyyy a\\t h:mmtt",
GetLowerCaseInfo());

Here's a complete program to demonstrate:

using System;
using System.Collections.Generic;
using System.Globalization;

public class Test
{
static void Main()
{
Console.WriteLine(DateTime.Now.ToString("dddd, MMMM d, yyyy a\\t h:mmtt",
GetLowerCaseInfo());
}

private static readonly Dictionary<DateTimeFormatInfo,DateTimeFormatInfo> cache =
new Dictionary<DateTimeFormatInfo,DateTimeFormatInfo>();

private static object cacheLock = new object();

public static DateTimeFormatInfo GetLowerCaseInfo()
{
DateTimeFormatInfo current = CultureInfo.CurrentCulture.DateTimeFormat;
lock (cacheLock)
{
DateTimeFormatInfo ret;
if (!cache.TryGetValue(current, out ret))
{
ret = (DateTimeFormatInfo) current.Clone();
ret.AMDesignator = ret.AMDesignator.ToLower();
ret.PMDesignator = ret.PMDesignator.ToLower();
cache[current] = ret;
}
return ret;
}
}
}

How to make pythons datetime object show AM and PM in lowercase?

Try changing '%p' to '%P'. Whether '%P' is supported depends on the 'strftime' function provided by the platform C library.

Display current time in 12 hour format with AM/PM

Easiest way to get it by using date pattern - h:mm a, where

  • h - Hour in am/pm (1-12)
  • m - Minute in hour
  • a - Am/pm marker

Code snippet :

DateFormat dateFormat = new SimpleDateFormat("hh:mm a");

Read more on documentation - SimpleDateFormat java 7

How do you format a datetime to am/pm, without periods, when using date-fns version 2.x?

You can now use the aaa pattern for that case (v.2.23.0).

Sample Image

Source: https://date-fns.org/v2.23.0/docs/format

Android DateFormat for AM/PM differs between devices

You may either have to use either the 24 hour value to determine what to append so that you can add the format you desire.

public static final String TIME = "hh:mm";

and then

String ampm = Integer.parseInt(time.valueOf("hh")) >= 12 ? "PM" : "AM";
...
return mDateFormat.format(date)+" "+ampm;

Or if you feel lazy you can just do without changing the value of TIME:

return mDateFormat.format(date).toUpperCase().replace(".","");

How to show the time as lower case am/pm

Consider converting it to string and calling the ToLower() method...

e.g...

string time = start.ToString().ToLower();



Related Topics



Leave a reply



Submit