Skip to main content

Python type() Function

When learning Python, you will work with different types of data, such as numbers, text, decimal values, and Boolean values. Python provides several built-in functions to help you work with these data types. One of the most useful functions is the type() function . The type() function tells you the data type of a value or variable. It helps you understand what kind of data Python is working with and can make debugging your programs easier. For example, consider these variables: age = 20 price = 99.50 name = "Rahul" is_student = True Although all four variables store data, their data types are different. Python allows us to check these types using type() . Let's understand how it works with simple examples. 1. What Is the type() Function in Python? The type() function is a built-in Python function that returns information about the type of an object. In beginner-level programming, you can use it to check whether a value is an integer, a floating-point number, a string,...

JavaScript Date and Time

Almost every modern application works with dates and times.

Examples include:

  • Attendance systems

  • Online booking platforms

  • Railway reservation systems

  • Calendar applications

  • Event management software

  • Banking applications

Imagine creating:

  • A digital clock

  • A countdown timer

  • An age calculator

  • A reminder application

All of these require date and time functionality.

JavaScript provides a built-in Date Object that allows developers to work with dates and times easily.

In this article, we will learn:

  • What the Date Object is

  • Creating dates

  • Current date and time

  • Getting date components

  • Setting date components

  • Formatting dates

  • Time calculations

  • Digital Clock Project

  • Countdown Timer

  • Age Calculator


What is the Date Object?

The Date object is a built-in JavaScript object used to work with:

  • Dates

  • Times

  • Timestamps

Example:

let today =
new Date();

console.log(today);

Output:

Current Date and Time

Why Date Object is Needed

Without Date Object:

Cannot determine:

Current Time

Current Date

Age

Days Between Dates

With Date Object:

Easy Date and Time Manipulation

Creating a Date Object

Syntax:

let date =
new Date();

Example:

let today =
new Date();

console.log(today);

Output:

Mon Jun 22 2026 ...

(The exact output depends on current date and time.)


Creating Specific Dates

Example:

let date =
new Date(
2026,
5,
22
);

Output:

June 22, 2026

Understanding Month Index

Months start from:

January = 0

February = 1

March = 2

...

December = 11

Example:

new Date(
2026,
0,
1
);

Represents:

January 1, 2026

Creating Date from String

Example:

let date =
new Date(
"2026-06-22"
);

Output:

June 22, 2026

Current Timestamp

Example:

let timestamp =
Date.now();

console.log(timestamp);

Output:

Milliseconds since
January 1, 1970

Getting Date Components

The Date object provides methods to retrieve information.


getFullYear()

Example:

let date =
new Date();

console.log(
date.getFullYear()
);

Output:

2026

getMonth()

Example:

console.log(
date.getMonth()
);

Output:

5

Represents June.


getDate()

Returns day of month.

Example:

console.log(
date.getDate()
);

Output:

22

getDay()

Returns day of week.

Example:

console.log(
date.getDay()
);

Output:

1

Meaning:

Sunday = 0

Monday = 1

Tuesday = 2

...

Getting Time Components


getHours()

console.log(
date.getHours()
);

Output:

15

(3 PM)


getMinutes()

console.log(
date.getMinutes()
);

Output:

45

getSeconds()

console.log(
date.getSeconds()
);

Output:

30

Date Components Overview

MethodDescription
getFullYear()Year
getMonth()Month
getDate()Day
getDay()Weekday
getHours()Hour
getMinutes()Minutes
getSeconds()Seconds

Setting Date Components

Date values can be modified.


setFullYear()

Example:

let date =
new Date();

date.setFullYear(
2030
);

console.log(date);

setMonth()

date.setMonth(
11
);

Sets month to December.


setDate()

date.setDate(
25
);

Sets day to 25.


Formatting Dates

Default output:

Mon Jun 22 2026

Sometimes custom formatting is needed.


Example

let date =
new Date();

let formatted =
date.getDate() +
"/" +
(date.getMonth()+1) +
"/" +
date.getFullYear();

console.log(formatted);

Output:

22/6/2026

Converting to String

let date =
new Date();

console.log(
date.toDateString()
);

Output:

Mon Jun 22 2026

Converting to Time String

console.log(
date.toTimeString()
);

Output:

15:45:30 GMT...

Locale Date Format

console.log(
date.toLocaleDateString()
);

Output:

22/06/2026

Locale Time Format

console.log(
date.toLocaleTimeString()
);

Output:

3:45:30 PM

Date Difference

Example:

let date1 =
new Date(
"2026-01-01"
);

let date2 =
new Date(
"2026-01-10"
);

let difference =
date2 - date1;

console.log(
difference
);

Output:

Milliseconds Difference

Convert Milliseconds to Days

let days =
difference /
(
1000 *
60 *
60 *
24
);

console.log(days);

Output:

9

Practical Project – Digital Clock

HTML:

<h1 id="clock"></h1>

JavaScript:

function updateClock()
{
    let now =
    new Date();

    document
    .getElementById(
    "clock"
    )
    .innerHTML =
    now.toLocaleTimeString();
}

setInterval(
updateClock,
1000
);

Output

10:30:01 AM

10:30:02 AM

10:30:03 AM

Updates every second.


Understanding setInterval()

Syntax:

setInterval(
function,
milliseconds
);

Example:

setInterval(
showTime,
1000
);

Runs every:

1000 ms

= 1 second

Practical Project – Countdown Timer

let count = 10;

let timer =
setInterval(
function()
{
    console.log(count);

    count--;

    if(count < 0)
    {
        clearInterval(
        timer
        );
    }
},
1000
);

Output:

10

9

8

...

0

Practical Project – Age Calculator

let birthYear =
2005;

let currentYear =
new Date()
.getFullYear();

let age =
currentYear -
birthYear;

console.log(age);

Output:

21

More Accurate Age Calculation

let birthDate =
new Date(
"2005-07-06"
);

let today =
new Date();

let age =
today.getFullYear()
-
birthDate.getFullYear();

console.log(age);

Practical Project – Greeting Based on Time

let hour =
new Date()
.getHours();

if(hour < 12)
{
    console.log(
    "Good Morning"
    );
}
else if(hour < 18)
{
    console.log(
    "Good Afternoon"
    );
}
else
{
    console.log(
    "Good Evening"
    );
}

Output

Depending on current time:

Good Morning

or

Good Afternoon

or

Good Evening

Complete Program

<!DOCTYPE html>

<html>

<body>

<h1 id="clock">

</h1>

<script>

function showTime()
{
    let now =
    new Date();

    document
    .getElementById(
    "clock"
    )
    .innerHTML =
    now.toLocaleTimeString();
}

setInterval(
showTime,
1000
);

</script>

</body>

</html>

Real-Life Applications

Date and Time are used in:

  • Railway Reservation Systems

  • Banking Applications

  • Attendance Management

  • Calendar Apps

  • Flight Booking Systems

  • Event Scheduling Platforms

Every modern application depends heavily on date and time calculations.


Common Mistakes Beginners Make

Forgetting Month Starts at 0

Wrong assumption:

January = 1

Correct:

January = 0

Using getDay Instead of getDate

Wrong:

date.getDay();

Returns weekday.

Correct:

date.getDate();

Returns day of month.


Forgetting Time Zone Differences

Different users may see different times based on location.


Best Practices

  • Use Date object for all date operations.

  • Use toLocaleDateString() for user-friendly dates.

  • Use timestamps for calculations.

  • Validate user-entered dates.

  • Be aware of time zones.


Advantages of Date Object

  • Easy date calculations

  • Built-in support

  • Flexible formatting

  • Time tracking

  • Essential for real-world applications


Conclusion

The JavaScript Date Object is a powerful tool for working with dates and times. It enables developers to create clocks, calendars, timers, booking systems, and scheduling applications.

Understanding date manipulation is an essential skill for modern web development.

Comments

Popular posts from this blog

Understanding Comments in Java: A Beginner’s Guide

When learning Java programming, one of the first concepts you’ll come across is comments . Comments may not affect how a program runs, but they play a crucial role in making code more understandable, readable, and maintainable. Let’s dive deeper into what comments are, why they’re important, and the different types of comments in Java. What are Comments in Java? A comment in Java is a piece of text written within a program that is ignored by the compiler . In other words, comments are notes for humans, not instructions for machines. They help developers explain the purpose of code, describe logic, or temporarily disable certain lines of code without deleting them. Why are Comments Important? 📌 Readability – Comments make code easier to understand for you and others. 📌 Maintenance – When you revisit your code after months, comments remind you what you were doing. 📌 Collaboration – In team projects, comments explain code to fellow developers. 📌 Debugging – You c...

Java Program to Check Voting Eligibility

In this program, we’ll take the user’s age as input and check whether the person is eligible to vote or not. In India, the minimum age required to vote is 18 years . 💻 Program Code // Java Program to Check Voting Eligibility import java.util.Scanner; public class VotingEligibility { public static void main(String[] args) { // Create Scanner object to take input from user Scanner sc = new Scanner(System.in); // Ask user to enter age System.out.print("Enter your age: "); int age = sc.nextInt(); // Check eligibility if (age >= 18) { System.out.println("You are eligible to vote."); } else { System.out.println("You are not eligible to vote yet."); System.out.println("You can vote after " + (18 - age) + " year(s)."); } sc.close(); // Close the scanner } } 🧠 Explanation import java.util.Scanner; → Imp...

Java Program to Calculate Total Marks and Average of Five Subjects

💡 Program Explanation In this program: The user will input marks of five subjects . The program will calculate the total marks and average . It will then display both results clearly. 🧑‍💻 Java Code: // Java Program to Calculate Total Marks and Average of Five Subjects import java.util.Scanner; // Importing Scanner class for user input public class TotalAndAverage { public static void main(String[] args) { // Creating Scanner object to take input from user Scanner sc = new Scanner(System.in); // Declaring variables for five subjects double subject1, subject2, subject3, subject4, subject5; // Taking marks input for each subject System.out.print("Enter marks of Subject 1: "); subject1 = sc.nextDouble(); System.out.print("Enter marks of Subject 2: "); subject2 = sc.nextDouble(); System.out.print("Enter marks of Subject 3: "); subject3 = sc.nextDo...