JavaScript Date toisostring() method: How to use toisostring() in JS

Do you want to master JavaScript Dates with the toISOString() method? Read on!

This article explores the toISOString() method in JavaScript, which converts Date objects into strings in ISO 8601 format.

You will learn how to use it, understand its parameters and return value, and discover how to convert string dates to ISO dates.

What is the toISOString method?

The toISOString() method is a part of the JavaScript Date prototype.

It returns a string representing the date in the date time string format, which is a simplified format based on ISO 8601.

The returned string is always 24 or 27 characters long (YYYY-MM-DDTHH:mm:ss.sssZ or ±YYYYYY-MM-DDTHH:mm:ss.sssZ).

The timezone is always UTC, as denoted by the suffix Z.

Syntax

dateObj.toISOString() 

Parameters

None

This toISOString() method does not take any parameters.

Return value

The toISOString() method returns a string in simplified extended ISO format (ISO 8601), which is always 24 or 27 characters long (YYYY-MM-DDTHH:mm:ss.sssZ or ±YYYYYY-MM-DDTHH:mm:ss.sssZ, respectively).

The timezone is always zero UTC offset, as denoted by the suffix “Z.”

Here’s an example of how to use it:

const date = new Date();
console.log(date.toISOString());

In our example date is a Date object. The toISOString() method is called on date, and it returns a string representing the date and time at which date was created, in ISO format. The date and time are in the UTC timezone.

When you run this code, it will print the current date and time in the ISO 8601 extended format (YYYY-MM-DDTHH:mm:ss.sssZ), and the time will be in Coordinated Universal Time (UTC).

📌Please note that the actual output will depend on the exact moment when you run this code.

Output:

2023-08-17T08:06:29.937Z

How to convert string date to ISO date in JavaScript?

You can convert a string date to an ISO date using the Date object and the toISOString() method.

Here’s how you can do it:

let dateStr = "August 17, 2023 04:17:32"; // your date string
let dateObj = new Date(dateStr); ✅ // create a new Date object
let isoStr = dateObj.toISOString(); ✅  // convert to ISO string

console.log(isoStr); 

In this example, dateStr is a string representing a date.

The Date object is used to convert this string into a date object (dateObj).

Then, the toISOString() method is called on dateObj to convert it to an ISO string (isoStr).

Output:

2023-08-17T04:17:32.000Z

Please note that the toISOString() method returns the date in the UTC timezone, denoted by the “Z” at the end of the string. If your original date string represents a date-time in a different timezone, you may need to adjust for this.

What is the T and Z in JavaScript datetime?

The “T” and “Z” in date and time strings come from the ISO 8601 date standard in JavaScript.

The “T” is a delimiter that separates the date from the time.

The “Z” stands for “Zulu time,” which is also known as Coordinated Universal Time (UTC).

When you see a “Z” at the end of a time, it indicates that time is in UTC. So, a time formatted as “2023-08-17T09:14:05Z” represents August 17, 2023 at 09:14:05 UTC.

How to remove t and z from date in JavaScript?

To remove the “T” and “Z” from a date string in JavaScript, you can use the replace() method with a regular expression.

Here’s an example:

let date = new Date();
let isoString = date.toISOString();

// Remove "T" and "Z"
let sample = isoString.replace(/T|Z/g, ' ');

console.log(sample); 

As you can see in our example, replace(/T|Z/g, ‘ ‘) replaces all occurrences of “T” and “Z” in the string with a space.

The g flag in the regular expression ensures that all occurrences are replaced, not just the first one.

Please note that this will also remove the “Z” that denotes the time is in UTC.

Conclusion

In conclusion, the toISOString() method is a powerful tool in JavaScript for handling and manipulating dates.

We’ve also explored how to convert string dates to ISO dates, the significance of “T” and “Z” in JavaScript datetime, and how to remove them from date strings.

With this knowledge, you can now effectively manage dates in your JavaScript applications.

We are hoping that this article provides you with enough information that helps you understand toisostring.

If you want to dive into more JavaScript topics, check out the following articles:

Thank you for reading Itsourcecoders 😊.

Frequently Asked Questions

Is JavaScript still worth learning in 2026?
Yes. JavaScript runs on 98% of websites for the front-end, dominates the back-end via Node.js, powers mobile apps through React Native, builds desktop tools through Electron, and is the scripting layer for most AI tooling (LangChain.js, OpenAI SDK, Vercel AI). Whether you target web, mobile, AI, or full-stack capstones, JavaScript is the broadest single language you can learn.
What is the difference between var, let, and const?
var is function-scoped, hoisted to the top of its scope, and can be redeclared, which leads to bugs in modern code. let is block-scoped (only visible inside the nearest {}) and can be reassigned. const is block-scoped and cannot be reassigned, although object contents can still mutate. Default to const for everything, switch to let only when you actually need to reassign, and avoid var in any code written after 2017.
Which JavaScript version should I target in 2026?
Target ES2020 (ES11) as the safe baseline because every modern browser and Node.js 14+ supports it fully. ES2022 adds useful features like top-level await, private class fields with the # prefix, and the .at() array method. If you are writing for older browsers (IE11 or older Android WebViews), transpile down with Babel or use a build tool like Vite, esbuild, or webpack.
What is the best free editor for JavaScript?
Visual Studio Code is the industry standard, free, with built-in IntelliSense, debugger, terminal, Git, and a huge extension marketplace (ESLint, Prettier, GitHub Copilot, Tailwind). Install the JavaScript and TypeScript Nightly extension for the latest language features. JetBrains WebStorm is more powerful and free for students with a verified .edu email. For quick scratchpad work, the Chrome DevTools Sources panel includes a workspace and breakpoint debugger.
How do I run JavaScript locally vs in the browser?
In the browser: open DevTools with F12 (or right-click then Inspect), go to the Console tab, type or paste your code, press Enter. For HTML pages, add a script tag pointing to your .js file. Locally with Node.js: download Node from nodejs.org (LTS version), then run node script.js in your terminal from the file folder. Use the same Node setup for backend capstones, API integrations, and scripts that do not need a browser.
What can I build with JavaScript for my BSIT capstone?
Common BSIT capstones in JavaScript: full-stack web apps using React or Vue on the front-end with Node.js and Express on the back-end (MongoDB or MySQL for the database), real-time chat or notification systems using Socket.io, single-page dashboards with Chart.js or D3.js, cross-platform mobile apps with React Native, AI-powered chatbots using OpenAI SDK and LangChain.js, and Chrome extensions for productivity tools. Add Tailwind CSS for the UI and Vercel or Netlify for free deployment.

Leave a Comment