Date formats are one of the most used features in coding in any programming language. The date and time are not the same in every region of the world; for example, if you are on Tuesday, countries on the other side of the world are on Monday or Wednesday. Because of the vast disparities in time zones, programmers incorporate date functions in their programs to represent the data in the computer’s local time zone.
The date format in which the data is recorded on a computer, on the other hand, is typically unorganised and difficult to comprehend, which is why date formats exist.
What are Date Formats?
Date format, in technical terms, is the format in which the date, month, and year of the date are represented in a specified format, such as ‘dd/mm/yy’ or ‘yy/dd/mm.’ Different systems and programming languages have different formats.
Each programming language has its collection of functions for displaying dates in a particular format.
Let’s look at the various date formatting functions available in various programming languages.
PHP Date Format
The date() function in PHP represents the date in a specific format. The syntax is as follows:
Syntax
date(format, timestamp)
Here,
- The format refers to the specified format in which the date will be represented.
- The timestamp is an optional parameter in which you can specify the timestamp at which the time and date should be presented. If not used, the function will return the current date.
Now let’s look at the formatting options which can be added to the function to display the dates in various formats,
- d refers to the date of the month, which will be represented in two digits (01 or 31).
- D refers to the day of the week, which will be represented in abbreviated texts (Mon to Sun).
- m refers to the month, which will be represented in numerals (01 or 12).
- M refers to the month, which will be represented in abbreviated texts (Jan to Dec).
- y refers to the year which will be represented in the last two digits (20 or 21)
- Y refers to the year which will be represented in four digits (2020 or 2021)
Example:
<?php
echo "The current date in different formats:" . "\n";
echo date("d/m/Y") . "\n";
echo date("d-m-Y") . "\n";
echo date("d.m.Y") . "\n";
echo date("d.M.Y/D");
?>
Output:
The current date in different formats:
21/06/2022
21-06-2022
21.06.2022
21.Jun.2022/Tue
Javascript Date Format
In Javascript, the new date() function represents the date. The syntax is as follows,
Syntax:
new Date();
new Date(value);
new Date(dateString);
Here,
- The value refers to the number of milliseconds since January 1, 1970, 00:00:00 UTC.
- The date string refers to the date format in which you want the date to be represented.
The date in Javascript can be formatted using the following ways,
- ISO Date
“2022-06-22” (The International Standard)
- Short Date
“06/22/2022”
- Long Date
“Jun 22 2022” or “22 Jun 2022”
Example:
<script>
var A = new Date('June 22, 2022');
document.write(A);
</script>
Output:
Wed Jun 22, 2022, 00:00:00 GMT+0200 (Central European Summer Time)
SQL Date Format
In SQL, the FORMAT function returns the current data in different specified formats. The syntax is as follows,
Syntax:
FORMAT (value,format[,culture])
GO
Here,
- In the value section, the function getdate() to get the date from the date column.
- In the format section, the options are specified how the date should be represented are specified.
- The culture is used to represent the date in a local culture setting.
Here is the list of parameters used to represent the data in a specific setting,
- dd, refers to the day of the month represented in numeric values. (01-31)
- dddd, refers to the current day represented in the text. (Monday – Wednesday)
- MM, refers to the month represented in numerics. (01-12)
- MMM, refers to the month name represented in abbreviation. (Jan – Dec)
- MMMM, refers to the month name represented in the text. (January – December)
- yy, refers to the year represented in the last two digits. (21 or 22)
- yyyy, refers to the year represented in four digits. (2021 or 2022)
Example:
SELECT FORMAT (getdate(), 'dd-MM-yy') as date
GO
Output:
22-06-22
Java Date Format
In java, under the LocalDate class, the now() method displays the current date.
To format the date, DateTimeFormatter class is imported, and the ofPattern() method displays the date in a specific format.
Example:
public class Time {
public static void main(String[] args) {
LocalDateTime currentDate = LocalDateTime.now();
System.out.println("Date Format before formatting " + currentDate);
DateTimeFormatter newDateFormat = DateTimeFormatter.ofPattern("dd-MM-yyyy");
String dateFormatted = currentDate.format(newDateFormat);
System.out.println("Date Format after formatting: " + dateFormatted);
}
Output:
Date Format before formatting: 2022-06-21T14:41:01.242164
Date Format after formatting: 21-06-2022
Here is the list of parameters used to represent the data in a specific setting,
- G, refers to the era. (AD)
- y, refers to the year represented in four digits. (2022)
- M, refers to the current month. (June or 06)
- d, refers to the current date. (01-31)
- E, refers to the day in a week. (Tuesday)
Python Date Format
The strftime() method is used in Python to format the date. The syntax is as follows,
Syntax:
time.strftime(format)
Here,
The format refers to the format in which the date will be represented.
Here is a rundown of the parameters which will be used to represent the data in a specific format,
- %Y, refers to the year represented in four digits. (2021)
- %y, refers to the year represented in the last two digits. (21)
- %m, refers to the month represented in two digits. (01-12)
- %B, refers to the month represented in the text. (January to December)
- %b, refers to the month represented in abbreviated form. (Jan to Dec)
- %A, refers to weekdays represented in the text. (Monday to Sunday)
- %a, refers to weekdays represented in short form. (Mon to Sun)
- %w, refers to weekdays represented in numbers. (0-6)
- %d, refers to the date represented in numbers.(01-31)
Example:
from datetime import datetime
time = datetime.now()
print("Without formating:", time)
print("After formating:", time.strftime("%b %d, %Y"))
Output:
Without formating: 2022-06-21 14:57:29.616289
After formating: Jun 21, 2022
Conclusion
Using the date format function, programmers can easily alter how they want the date to be represented on their projects. With the mentioned date formats, try altering the formats of date in your programs.
Learn coding basics for free
If you’re new to software development, some of the terms in this article may seem daunting. However, anyone can learn to code. Try our free 5 Day Coding Challenge and learn the basics of HTML, CSS and JavaScript. Register now through the form below.