Skip to content
the poor coder

xlsx date js

Poor Man
Published
Reading time
2 min read
On this page

XLSX Date JS#

If you are working with XLSX files, you might need to manipulate dates in the file using JavaScript. Here are some ways to achieve that:

Using Moment.js#

Moment.js is a popular library used for working with dates and times in JavaScript. You can use Moment.js to parse dates from XLSX files and manipulate them as needed.

Text

const moment = require('moment');

// Parse date from XLSX
const dateStr = '2021-05-25T00:00:00.000Z';
const date = moment(dateStr).toDate();

// Format date
const formattedDate = moment(date).format('DD/MM/YYYY');

Using Date Object#

You can also use the built-in Date object in JavaScript to work with dates from XLSX files. However, you will need to parse the date string manually using methods like substring or split.

Text

// Parse date from XLSX
const dateStr = '2021-05-25T00:00:00.000Z';
const year = dateStr.substring(0, 4);
const month = dateStr.substring(5, 7);
const day = dateStr.substring(8, 10);
const date = new Date(year, month - 1, day);

// Format date
const formattedDate = `${date.getDate()}/${date.getMonth() + 1}/${date.getFullYear()}`;

Using XLSX.js#

If you are using the XLSX.js library to read XLSX files in your JavaScript code, you can use the XLSX.SSF.parse_date_code method to parse date codes from the file and convert them to JavaScript Date objects.

Text

const XLSX = require('xlsx');

// Parse date from XLSX
const dateCode = 44147; // Example date code from file
const date = XLSX.SSF.parse_date_code(dateCode);

// Format date
const formattedDate = `${date.getDate()}/${date.getMonth() + 1}/${date.getFullYear()}`;

Conclusion#

There are multiple ways to work with dates from XLSX files in JavaScript, depending on your preference and the libraries you are using. Whether you choose to use Moment.js, the Date object, or XLSX.js, make sure to parse the date string correctly and format it as needed for your use case.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC