ayeonChoi
ayeonChoi

Reputation: 1

How do I change the 'YYMMDD' - > 'YYYY-MM-DD' in dayjs?

How to change YYMMDD -> YYYY-MM-DD in dayjs

const startDateString = dayjs(createdAtStart).startOf('day').format() const endDateString = dayjs(createdAtEnd).endOf('day').format()

Upvotes: 0

Views: 1620

Answers (3)

Jax-p
Jax-p

Reputation: 8551

According to the documentation use .format('YYYY-MM-DD').

const formated = dayjs('1. 1. 2023').format('YYYY-MM-DD');
console.log(formated);
<script src="https://cdnjs.cloudflare.com/ajax/libs/dayjs/1.11.7/dayjs.min.js" integrity="sha512-hcV6DX35BKgiTiWYrJgPbu3FxS6CsCjKgmrsPRpUPkXWbvPiKxvSVSdhWX0yXcPctOI2FJ4WP6N1zH+17B/sAA==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

Upvotes: 1

Sam020
Sam020

Reputation: 381

I think what you are looking for (if dayJS doesn't parse YYMMDD out of the box) is the following plugin: custom_parse_format.

First you add the plugin (see docs under "Plugins" how to load plugin for NodeJS or browser):

var customParseFormat = require('dayjs/plugin/customParseFormat')
dayjs.extend(customParseFormat)

Then you can parse a custom date format like YYMMDD:

dayjs(createdAtStart, 'YYMMDD').format('YYYY-MM-DD')

Of course, you can replace format with any other method or chain of methods.

Upvotes: 2

xbhuang
xbhuang

Reputation: 1

const dateString1 = "20230209150300";
const pattern1 = /(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/;
const formatedDate1 = dateString1.replace(pattern1, '$1-$2-$3 $4:$5:$6');

Upvotes: 0

Related Questions