Here’s an overview of how to print out current time with the format hours:minutes:seconds in multiple programming languages…
C/C++:
#include <time.h>
#include <stdio.h>
int main(void) {
struct tm *ptr;
time_t lt;
char str[80];
lt = time(NULL);
ptr = localtime(<);
strftime(str, 100, "%H:%M:%S", ptr);
printf(str);
return 0;
}
C#:
using System;
class Program {
static void Main() {
DateTime time = DateTime.Now;
string format = "HH:mm:ss";
Console.WriteLine(time.ToString(format));
}
}
Java:
import java.util.Date;
import java.text.SimpleDateFormat;
public class DateUtils {
public static void main(String arg[]) {
System.out.println(new SimpleDateFormat("hh:mm:ss").format(new Date()));
}
}
Visual Basic.NET
Dim timeInfo as DateTime = DateTime.Now
MessageBox.Show(DateTime.Now.ToString("HH:mm:ss"))
Tcl:
puts [clock format [clock seconds] -format {%H:%M:%S}]
Python:
import time
print time.strftime('%H:%M:%S')
PHP:
print date('H:i:s');
Perl:
($s, $m, $h) = localtime(); print "$h:$m:$s";
Ruby:
time = Time.new
puts time.strftime("%H:%M:%S")
JavaScript:
var d = new Date(); document.write(d.getHours() + ':' + d.getMinutes() + ':' + d.getSeconds());
Lua:
print(os.date("%H:%M:%S"))
ColdFusion:
<cfoutput> #timeformat(now(),'HH:mm:ss')# <cfoutput>