How to Convert 24 Hr Format Time in to 12 Hr Format

How to convert 24 hr format time in to 12 hr Format?

you can try using a SimpleDateFormat object to convert the time formats.

final String time = "23:15";

try {
final SimpleDateFormat sdf = new SimpleDateFormat("H:mm");
final Date dateObj = sdf.parse(time);
System.out.println(dateObj);
System.out.println(new SimpleDateFormat("K:mm").format(dateObj));
} catch (final ParseException e) {
e.printStackTrace();
}

here is the javadoc link for SimpleDateFromat.

How can I convert 24 hour time to 12 hour time?

>>> from datetime import datetime
>>> d = datetime.strptime("10:30", "%H:%M")
>>> d.strftime("%I:%M %p")
'10:30 AM'
>>> d = datetime.strptime("22:30", "%H:%M")
>>> d.strftime("%I:%M %p")
'10:30 PM'

Converting 24 hour time to 12 hour time w/ AM & PM using Javascript

UPDATE 2: without seconds option

UPDATE: AM after noon corrected, tested: http://jsfiddle.net/aorcsik/xbtjE/

I created this function to do this:

function formatDate(date) {  var d = new Date(date);  var hh = d.getHours();  var m = d.getMinutes();  var s = d.getSeconds();  var dd = "AM";  var h = hh;  if (h >= 12) {    h = hh - 12;    dd = "PM";  }  if (h == 0) {    h = 12;  }  m = m < 10 ? "0" + m : m;
s = s < 10 ? "0" + s : s;
/* if you want 2 digit hours: h = h<10?"0"+h:h; */
var pattern = new RegExp("0?" + hh + ":" + m + ":" + s);
var replacement = h + ":" + m; /* if you want to add seconds replacement += ":"+s; */ replacement += " " + dd;
return date.replace(pattern, replacement);}
alert(formatDate("February 04, 2011 12:00:00"));

Javascript: convert 24-hour time-of-day string to 12-hour time with AM/PM and no timezone

Nothing built in, my solution would be as follows :

function tConvert (time) {
// Check correct time format and split into components
time = time.toString ().match (/^([01]\d|2[0-3])(:)([0-5]\d)(:[0-5]\d)?$/) || [time];

if (time.length > 1) { // If time format correct
time = time.slice (1); // Remove full string match value
time[5] = +time[0] < 12 ? 'AM' : 'PM'; // Set AM/PM
time[0] = +time[0] % 12 || 12; // Adjust hours
}
return time.join (''); // return adjusted time or original string
}

tConvert ('18:00:00');

This function uses a regular expression to validate the time string and to split it into its component parts. Note also that the seconds in the time may optionally be omitted.
If a valid time was presented, it is adjusted by adding the AM/PM indication and adjusting the hours.

The return value is the adjusted time if a valid time was presented or the original string.

Working example

(function() {
function tConvert(time) { // Check correct time format and split into components time = time.toString().match(/^([01]\d|2[0-3])(:)([0-5]\d)(:[0-5]\d)?$/) || [time];
if (time.length > 1) { // If time format correct time = time.slice(1); // Remove full string match value time[5] = +time[0] < 12 ? 'AM' : 'PM'; // Set AM/PM time[0] = +time[0] % 12 || 12; // Adjust hours } return time.join(''); // return adjusted time or original string }
var tel = document.getElementById('tests');
tel.innerHTML = tel.innerHTML.split(/\r*\n|\n\r*|\r/).map(function(v) { return v ? v + ' => "' + tConvert(v.trim()) + '"' : v; }).join('\n');})();
<h3>tConvert tests : </h3><pre id="tests">  18:00:00  18:00  00:00  11:59:01  12:00:00  13:01:57  24:00  sdfsdf  12:61:54</pre>

Converting 24hour time to 12hour time?

Try using a SimpleDateFormat:

String s = "12:18:00";
DateFormat f1 = new SimpleDateFormat("HH:mm:ss"); //HH for hour of the day (0 - 23)
Date d = f1.parse(s);
DateFormat f2 = new SimpleDateFormat("h:mma");
f2.format(d).toLowerCase(); // "12:18am"

how to convert date and time to 12 hour format

You need two formats: one to parse, and one to format. You need to parse from String to Date with one DateFormat, then format that Date into a String with the other format.

Currently, your single SimpleDateFormat is half way between - you've got HH which is 24-hour, but you've also got aa which is for am/pm. You want HH without the aa for input, and hh with the aa for output. (It's almost never appropriate to have both HH and aa.)

TimeZone utc = TimeZone.getTimeZone("etc/UTC");
DateFormat inputFormat = new SimpleDateFormat("dd MMM, yyyy HH:mm",
Locale.US);
inputFormat.setTimeZone(utc);
DateFormat outputFormat = new SimpleDateFormat("dd MMM, yyyy hh:mm aa",
Locale.US);
outputFormat.setTimeZone(utc);

Date date = inputFormat.parse(input);
String output = outputFormat.format(date);

Note that I'm setting the locale to US so it can always parse "Nov", and the time zone to UTC so you don't need to worry about certain times being skipped or ambiguous.

Converting the database 24 hr clock to 12 hr clock

I just want to be clear about this. In .NET, a DateTime represents an instant in time. It is stored in memory in one specific way and your user interface, which is serving it up to the user can show it in many different ways. Just like if someone asks you for the time, you might say to them "It's 1400 hours" or you might say "It's 2pm" or you might say something like "It's a quarter after 3". You can answer the question many different ways.

Similarly, your User Interface can take a DateTime structure and display it many different ways:

DateTime myDate = new DateTime(2015, 10, 5, 17, 30, 00);
string twelveHour = myDate.ToString("h:mm tt"); // 5:30 PM
string twentyFourHour = myDate.ToString("HH:mm"); // 17:30

You can see more examples here. The thing to take away from this though is that you aren't "converting" the DateTime structure. What you are doing is composing a string, in a specific format, based on the DateTime.

SimpleDateFormat returns 24-hour date: how to get 12-hour date?

Change HH to hh as

long timeInMillis = System.currentTimeMillis();
Calendar cal1 = Calendar.getInstance();
cal1.setTimeInMillis(timeInMillis);
SimpleDateFormat dateFormat = new SimpleDateFormat(
"dd/MM/yyyy hh:mm:ss a");
dateforrow = dateFormat.format(cal1.getTime());

Note that dd/mm/yyyy - will give you minutes instead of the month.

Converting 24hour time format to 12hour time?

Maybe you can do something like this:

final String time = "12:18:00";

try {
final SimpleDateFormat sdf = new SimpleDateFormat("H:mm");
final Date dateObj = sdf.parse(time);
System.out.println(dateObj);
System.out.println(new SimpleDateFormat("K:mm").format(dateObj));
} catch (final ParseException e) {
e.printStackTrace();
}

Taken from here.



Related Topics



Leave a reply



Submit