How To Print/Get Today Date and Time In JavaScript, C/C++, Python, C#, Java, PHP, PowerShell, Excel (VBScript) ? – POFTUT

How To Print/Get Today Date and Time In JavaScript, C/C++, Python, C#, Java, PHP, PowerShell, Excel (VBScript) ?


While developing applications date and time operations are very important. A lot of different data and record is bind to a date or time. We can also use the only date which specifies the year, month and day or time which specifies the hour, minute, second. All popular programming languages provides date and time functions to get the current date and time. In this tutorial, we will learn today date and time in different programming and scripting languages.

Today Date and Time Use Cases

Today date and time can be used in different situations. We can use today or the current date and time like the following use cases.

  • While publishing posts in web sites today date and time used to time stamp the date the post is created and published.
  • In order to store the actions and their time to analyze later like login, shopping, shipment.
  • In order to sort according to date like photos, posts, etc we have to set their creation date and time.

Today Date and Time In JavaScript

JavaScript programming language provides today date and time with the new Date() function which will create a new date and time object which will store current date and time. We will use attributes like getFullYear() , getMonth() , getDate() , getHours() , getMinutes() , getSeconds() function like below.

var today = new Date();

var current_day = today.getDay();

var current_month = today.getMonth();

var current_year = today.getYear();

var current_hour = today.getHours();

var current_minute = today.getMinutes();

var current_second = today.getSeconds();
Today Date and Time In JavaScript
Today Date and Time In JavaScript

Today Date and Time In C/C++

C and C++ programming languages provides time.h headers with the time struct and localtime function. We can use these which will provide current or today date and time as tm type. Then tm_year , tm_mon , tm_today , tm_hour , tm_min, tm_sec struct elements can be used to get different data.

#include <assert.h>
#include <stdio.h>
#include <time.h>

int main(void) {
    time_t t = time(NULL);
    struct tm tm = *localtime(&t);
    printf("Year: %d\n", tm.tm_year + 1900);
    printf("Month: %d\n", tm.tm_mon + 1);
    printf("Day: %d\n", tm.tm_mday );
    printf("Hour: %d\n", tm.tm_hour);
    printf("Minute: %d\n", tm.tm_min);
    printf("Second: %d\n", tm.tm_sec);
    return 0;
}
Today Date and Time In C/C++
Today Date and Time In C/C++

Today Date and Time In Python

Python programming language provides date and time functions in the datetime module. We can use date object today() function in order to get current or today date and time like below. We will use strftime() function with formatting parameters which are like below.

  • `%d` is Day
  • `%m` is Month
  • `%Y` is Year
  • `%H` is Hour
  • `%M` is Minute
  • `%S` is Second
from datetime import datetime

now = datetime.now()

now.strftime("%d/%m/%Y %H:%M:%S")
Today Date and Time In Python
Today Date and Time In Python

Today Date and Time In C#

C# programming language provides DateTime library where Now attributes ToString() function can be used to get current today date and time and format properly into a string.

DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss")

Today Date and Time In Java

Java provides the java.util library which containsDate() function which will provide the current day and time. We will also use Date type which can hold all date attributes like an hour, day, etc. We will also use some format in order to print the current date and time in string format.

SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss "); 

Date date = new Date(System.currentTimeMillis()); 

System.out.println(formatter.format(date));

Today Date and Time In PHP

PHP provides date() function which will provide today date and time. We can also format this information with some formatters like below.

  • `Y` is Year
  • `m` is  Month
  • `d` is Day
  • `h` is Hour
  • `i` is Minute
  • `sa` is Second
now = date(Y-m-d h:i:sa);
Today Date and Time In PHP
Today Date and Time In PHP

Today Date and Time In PowerShell

PowerShell provides Get-Date command in order to print the current date and time. We have to use -UFormat option in order to print Day/Month/Year Hour:Minute:Secondformat like below.

  • `%Y` is Year
  • `%m` is Month
  • `%d` is Day
  • `%H` is Hour
  • `%M` is  Minute
  • `%S` is Second
PS> Get-Date -UFormat "%A %m/%d/%Y %R %Z"
Today Date and Time In PowerShell
Today Date and Time In PowerShell

Today Date and Time In Excel (VBScript)

Excel office application supports Macros written in VBScript. We can get the current or today date and time with the NOW() function. We also need to use = to make NOW() function calculate.

=NOW()

LEARN MORE  Linux Time Command Tutorial With Examples To Get Programs Resource Usage

Leave a Comment