Technology

Javascript Change 1:06 Pm to 13.06.00

Javascript Change 1:06 Pm to 13.06.00: Converting time formats in JavaScript is a common task, especially when working with user input, databases, or UI widgets. One frequent requirement is converting 12-hour time (e.g., 1:06 PM) into a 24-hour format with dots, such as 13.06.00.
This guide explains how to perform this conversion clearly, efficiently, and using clean JavaScript.


Understanding the Time Format Conversion

The goal is to convert:
1:06 PM → 13.06.00

This transformation includes three steps:

  1. Convert from 12-hour to 24-hour time

  2. Format hours, minutes, and seconds

  3. Replace separators with dots instead of colons

In JavaScript, this can be done using either built-in methods or manual string manipulation.


Using JavaScript’s Date Object for Conversion

One of the cleanest solutions is using the Date object.
Below is an example:

let input = "1:06 PM";
let date = new Date("1970-01-01 " + input);

let hours = String(date.getHours()).padStart(2, "0");
let minutes = String(date.getMinutes()).padStart(2, "0");
let seconds = String(date.getSeconds()).padStart(2, "0");

let result = `${hours}.${minutes}.${seconds}`;
console.log(result); // 13.06.00

Why this works

  • JavaScript automatically interprets PM/AM.

  • padStart ensures two-digit formatting.

  • Dot-separated formatting is easy to apply.


Manual Conversion Without Using Date()

If you prefer to avoid using the Date object, you can convert the time manually:

let input = "1:06 PM";

let [time, modifier] = input.split(" ");
let [h, m] = time.split(":");

if (h === "12") h = "00";
if (modifier === "PM") h = String(Number(h) + 12);

let result = `${h.padStart(2, "0")}.${m.padStart(2, "0")}.00`;
console.log(result); // 13.06.00

Manual method benefits

  • Works in environments with limited Date support

  • Great for custom time parsers

  • More control over formatting


Using Intl.DateTimeFormat for Locale-Based Conversions

Modern JavaScript allows advanced time formatting using Intl.DateTimeFormat:

let input = "1:06 PM";
let date = new Date("1970-01-01 " + input);

let formatted = new Intl.DateTimeFormat("en-GB", {
hour: "2-digit",
minute: "2-digit",
second: "2-digit",
hour12: false
})
.format(date)
.replace(/:/g, ".");

console.log(formatted); // 13.06.00

Why use Intl?

  • Handles formatting automatically

  • Supports multiple locales

  • Useful for apps requiring multilingual support


Common Mistakes to Avoid

When converting time formats in JavaScript, beware of:

1. Missing zero padding

Without padStart(2), results like 13.6.0 may occur.

2. Parsing times without AM/PM

JavaScript may misinterpret such strings as 24-hour format.

3. Local timezone issues

Always use a fixed date like 1970-01-01 to prevent unwanted timezone shifts.

4. Using invalid input formats

Make sure user input is always normalized before conversion.


Conclusion

Converting 1:06 PM into 13.06.00 in JavaScript is straightforward using built-in functions, manual logic, or international formatting tools. Whether you’re building a scheduling system, digital clock, log parser, or data transformation tool, these methods ensure accurate and clean time conversion.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button