Get Current Time in Hours and Minutes

Get current time in hours and minutes

Provide a format string:

date +"%H:%M"

Running man date will give all the format options

%a     locale's abbreviated weekday name (e.g., Sun)
%A locale's full weekday name (e.g., Sunday)
%b locale's abbreviated month name (e.g., Jan)
%B locale's full month name (e.g., January)
%c locale's date and time (e.g., Thu Mar 3 23:05:25 2005)
%C century; like %Y, except omit last two digits (e.g., 20)
%d day of month (e.g., 01)
%D date; same as %m/%d/%y
%e day of month, space padded; same as %_d
%F full date; same as %Y-%m-%d
%g last two digits of year of ISO week number (see %G)
%G year of ISO week number (see %V); normally useful only with %V
%h same as %b
%H hour (00..23)
%I hour (01..12)
%j day of year (001..366)
%k hour, space padded ( 0..23); same as %_H
%l hour, space padded ( 1..12); same as %_I
%m month (01..12)
%M minute (00..59)
%n a newline
%N nanoseconds (000000000..999999999)
%p locale's equivalent of either AM or PM; blank if not known
%P like %p, but lower case
%r locale's 12-hour clock time (e.g., 11:11:04 PM)
%R 24-hour hour and minute; same as %H:%M
%s seconds since 1970-01-01 00:00:00 UTC
%S second (00..60)
%t a tab
%T time; same as %H:%M:%S
%u day of week (1..7); 1 is Monday
%U week number of year, with Sunday as first day of week (00..53)
%V ISO week number, with Monday as first day of week (01..53)
%w day of week (0..6); 0 is Sunday
%W week number of year, with Monday as first day of week (00..53)
%x locale's date representation (e.g., 12/31/99)
%X locale's time representation (e.g., 23:13:48)
%y last two digits of year (00..99)
%Y year
%z +hhmm numeric time zone (e.g., -0400)
%:z +hh:mm numeric time zone (e.g., -04:00)
%::z +hh:mm:ss numeric time zone (e.g., -04:00:00)
%:::z numeric time zone with : to necessary precision (e.g., -04, +05:30)
%Z alphabetic time zone abbreviation (e.g., EDT)

How to get current hour and minutes in two digit using go?

For example,

package main

import (
"fmt"
"time"
)

func main() {
hours, minutes, _ := time.Now().Clock()
currUTCTimeInString := fmt.Sprintf("%d%02d", hours, minutes)
fmt.Println(currUTCTimeInString)
hours, minutes = 9, 0
currUTCTimeInString = fmt.Sprintf("%d%02d", hours, minutes)
fmt.Println(currUTCTimeInString)
hours, minutes = 10, 0
currUTCTimeInString = fmt.Sprintf("%d%02d", hours, minutes)
fmt.Println(currUTCTimeInString)
}

Output:

2300
900
1000

Or, using strconv,

package main

import (
"fmt"
"strconv"
)

func main() {
hours, minutes := 9, 00
h := strconv.Itoa(hours)
m := strconv.Itoa(minutes)
if minutes < 10 {
m = "0" + m
}
hm := h + m
fmt.Println(hm)
}

Output:

900

How to get current time in python and break up into year, month, day, hour, minute?

The datetime module is your friend:

import datetime
now = datetime.datetime.now()
print(now.year, now.month, now.day, now.hour, now.minute, now.second)
# 2015 5 6 8 53 40

You don't need separate variables, the attributes on the returned datetime object have all you need.

Javascript, Time and Date: Getting the current minute, hour, day, week, month, year of a given millisecond time

The variable names should be descriptive:

var date = new Date;
date.setTime(result_from_Date_getTime);

var seconds = date.getSeconds();
var minutes = date.getMinutes();
var hour = date.getHours();

var year = date.getFullYear();
var month = date.getMonth(); // beware: January = 0; February = 1, etc.
var day = date.getDate();

var dayOfWeek = date.getDay(); // Sunday = 0, Monday = 1, etc.
var milliSeconds = date.getMilliseconds();

The days of a given month do not change. In a leap year, February has 29 days. Inspired by http://www.javascriptkata.com/2007/05/24/how-to-know-if-its-a-leap-year/ (thanks Peter Bailey!)

Continued from the previous code:

var days_in_months = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
// for leap years, February has 29 days. Check whether
// February, the 29th exists for the given year
if( (new Date(year, 1, 29)).getDate() == 29 ) days_in_month[1] = 29;

There is no straightforward way to get the week of a year. For the answer on that question, see Is there a way in javascript to create a date object using year & ISO week number?

PHP Current time in Hour:Minutes

i think in this case it would be very use full to use the DateTime class from PHP. The Problem with your code is strtotime returns a int not an DateTime object.

I've modified your code so it will work:

$org = new DateTime("2018-01-07 16:35:10");
$then = $org->add(new DateInterval("PT6H"));
echo $then->format("H:i"),"<br>";
$afterThen = $then->add(new DateInterval("PT15M"));
echo $afterThen->format("H:i");

Is there any way to get current time and minutes from SQL to compare with date time?

For hours and minutes, there's just no "shortcut" function you can use - you need to resort to using DATEPART:

SELECT * 
FROM ScheduleTable
WHERE YEAR(CURRENT_TIMESTAMP) = YEAR(schedule)
AND MONTH(CURRENT_TIMESTAMP) = MONTH(schedule)
AND DAY(CURRENT_TIMESTAMP) = DAY(schedule)
AND DATEPART(HOUR, CURRENT_TIMESTAMP) = DATEPART(HOUR, schedule)
AND DATEPART(MINUTE, CURRENT_TIMESTAMP) = DATEPART(MINUTE, schedule)

See the official MS docs entry for DATEPART for more details

Python call datetime current hour?

You were pretty close to an answer. Here we go.

import datetime

after importing the datetime module you just need to call:

current_time = datetime.datetime.now()

In case you wanna access the data you have the year, month, day, hour, minute, second, microsecond methods:

current_time.day # Will return 17

To specify a given hour you just have to a variable you have the datetime.time class.

An idealized time, independent of any particular day, assuming that every day has exactly 246060 seconds. (There is no notion of “leap seconds” here.) Attributes: hour, minute, second, microsecond, and tzinfo.

start_time = datetime.time(17, 25, 30) # (17:25:30)

And the same as before. Accessing data can be done by calling its methods.

start_time.hour # will return 17

Here you have the documentation: :)
datetime module

python date time get the current time but with seconds and hour and minute

Use

>>> datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
'2016-06-26 14:15:44'

You can even use .today() instead of .now().

datetime.date.today() is of type <type 'datetime.date'> which basically returns only the date. That is why your hours and minutes when printed returns 00:00.

Hence, if you use datetime.datetime.now() this would return type <type 'datetime.datetime'> which is a datetime object, you'll get the date and the time.

Getting Hour and Minute in PHP

print date('H:i');
$var = date('H:i');

Should do it, for the current time. Use a lower case h for 12 hour clock instead of 24 hour.

More date time formats listed here.



Related Topics



Leave a reply



Submit