Get current time in hours and minutes

Learn get current time in hours and minutes with practical examples, diagrams, and best practices. Covers linux, bash, time development techniques with visual explanations.

Mastering Time: How to Get Current Hours and Minutes in Linux Bash

Hero image for Get current time in hours and minutes

Learn various methods to extract and format the current time in hours and minutes using Bash commands on Linux systems, from simple date commands to advanced formatting.

Retrieving the current time in a specific format is a common task in shell scripting and command-line operations. Whether you need it for logging, file naming, or displaying information, Bash provides powerful tools to manipulate and format date and time data. This article will guide you through different approaches to get just the hours and minutes from the current system time in Linux.

The date Command: Your Primary Tool

The date command is the most versatile utility for handling date and time in Linux. It allows for extensive formatting options, making it straightforward to extract specific components like hours and minutes. The key to its flexibility lies in its format specifiers, which begin with a percentage sign (%).

date +%H:%M

Basic date command to get current hours and minutes.

In the command above:

  • + tells date to output a custom format.
  • %H represents the hour (00-23) in 24-hour format.
  • %M represents the minute (00-59).
  • : is a literal character used to separate hours and minutes, as commonly seen.

Understanding Time Formatting Options

The date command offers a rich set of format specifiers. Understanding these can help you tailor the output precisely to your needs. Here's a brief overview of relevant specifiers for time:

  • %H: Hour (00-23)
  • %I: Hour (01-12)
  • %M: Minute (00-59)
  • %S: Second (00-60)
  • %p: Locale's equivalent of AM or PM
  • %R: Time in 24-hour HH:MM format (equivalent to %H:%M)
  • %T: Time in 24-hour HH:MM:SS format (equivalent to %H:%M:%S)
# Using %R for HH:MM
date +%R

# Using %I:%M %p for 12-hour format
date +%I:%M\ %p

# Getting hours only
date +%H

# Getting minutes only
date +%M

Examples of different time formatting options with date.

flowchart TD
    A[Start] --> B{Need Current Time?}
    B -- Yes --> C[Use `date` command]
    C --> D{Specific Format Needed?}
    D -- Yes --> E[Apply Format Specifiers]
    E --> F{"e.g., `+%H:%M`"}
    F --> G[Output Hours and Minutes]
    D -- No --> H[Default `date` output]
    H --> G
    G --> I[End]

Workflow for obtaining current time in hours and minutes using date.

Storing Time in a Variable

Often, you'll want to store the current time in a shell variable for later use within a script. This is a straightforward process using command substitution.

CURRENT_TIME=$(date +%H:%M)
echo "The current time is: $CURRENT_TIME"

# Example with 12-hour format
CURRENT_TIME_12H=$(date +%I:%M\ %p)
echo "The current time (12-hour) is: $CURRENT_TIME_12H"

Storing formatted time in Bash variables.