Open In App

Formats for date and dateTime in JSON payloads

Last Updated : 17 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

JSON stands for JavaScript Object Notation. It is a text-based data exchange format that allows you to transfer data between different languages and platforms. JavaScript is commonly used to interact with JSON files. we will explore the Format for date and dateTime in JSON payloads in different ways.

These are the following ways:

Using ISO 8601 Format

In this approach, A JSON payload with two properties: “date” and “datetime”, both containing strings formatted according to the ISO 8601 standard. The “date” property holds the date “2024-04-13”, while the “datetime” property holds the date and time “2024-04-13T08:30:00Z”. When you stringify the JSON payload using JSON.stringify(), it will produce a valid JSON string.

Example: The code below shows the Format for date and dateTime in JSON payloads using ISO 8601 Format.

JavaScript
const isoDate = "2024-04-13";
const isoDateTime = "2024-04-13T08:30:00Z";

const jsonPayload = {
    "date": isoDate,
    "datetime": isoDateTime
};

console.log(JSON.stringify(jsonPayload));

Output
{"date":"2024-04-13","datetime":"2024-04-13T08:30:00Z"}

Using Unix Timestamp

In this approach, a Unix timestamp represents the current time (in seconds since the Unix epoch) and assigns it to the “timestamp” property of a JSON payload. It can stringify the JSON payload using it JSON.stringify(), it will produce a valid JSON string with the Unix timestamp included. Unix timestamp represents the number of seconds elapsed since the Unix epoch (January 1, 1970, 00:00:00 UTC).

Example: The code below shows Format for date and dateTime in JSON payloads using Unix Timestamp.

JavaScript
const unixTimestamp = Math.floor(new Date()
                          .getTime() / 1000);

const jsonPayload = {
    "timestamp": unixTimestamp
};

console.log(JSON.stringify(jsonPayload));

Output
{"timestamp":1712988368}

Using Custom Format

In this approach, create Date objects for the current date and time, format them into custom strings representing date and datetime, and then construct a JSON payload with these formatted strings. Finally, it logs the JSON payload as a string using JSON.stringify(). You can choose custom date and datetime formats based on your specific requirements by using the method of Date() objects.

Example: The code below shows Format for date and dateTime in JSON payloads

JavaScript
// Create Date objects representing
// the current date and time
const currentDate = new Date();
const currentDateTime = new Date();

// Format date and datetime strings
const formattedDate = `${currentDate.getMonth() + 1}/
                       ${currentDate.getDate()}/${currentDate.
                        getFullYear()}`;
const formattedDateTime = `${currentDateTime.getMonth() + 1
                            }/${currentDateTime.getDate()}
                           /${currentDateTime.getFullYear()}
                           ${currentDateTime.getHours()}:
                           ${currentDateTime.getMinutes()}:
                           ${currentDateTime.getSeconds()}`;

// Create JSON payload
const jsonPayload = {
    "current_date": formattedDate,
    "current_datetime": formattedDateTime
};

console.log(JSON.stringify(jsonPayload));

Output
{"current_date":"4/13/2024","current_datetime":"4/13/2024 6:14:55"}

Using Human Readable Format using new Date()

In this approach, The current date, month, day, and year, construct a human-readable date string in the format “Month Day, Year”, creates a JSON payload with this string, and logs the JSON payload as a string using JSON.stringify(). We can use human-readable formats such as MM/DD/YYYY or Month Day, Year in JSON payloads for presentation purposes.

Example: The code below shows Format for date and dateTime in JSON payloads using Human Readable Format using new Date()

JavaScript
// Get the current date
const currentDate = new Date();

// Get the month name
const monthNames = [ "January", "February", 
                     "March", "April", "May", "June",
                     "July", "August", "September", 
                     "October", "November", "December"
];
const currentMonth = monthNames[currentDate.getMonth()];

// Get the day of the month
const currentDay = currentDate.getDate();

// Get the year
const currentYear = currentDate.getFullYear();

// Construct the current day string
const currentDayString = currentMonth + " " + currentDay + 
                                        ", " + currentYear;

// Create the JSON payload
const jsonPayload = {
    "human_readable_date": currentDayString
};

// Output the JSON payload
console.log(JSON.stringify(jsonPayload));

Output
{"human_readable_date":"April 13, 2024"}


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads