Convert date string of format `YYYYMMDD` into `DD/MM/YYYY` [duplicate] - javascript

This question already has answers here:
Parsing a string to a date in JavaScript
(35 answers)
Why does Date.parse give incorrect results?
(11 answers)
Closed 2 years ago.
I have an xml that gives me the date as a text in reverse "20200528". I need to change that string in a date format DD/MM/YYYY in JavaScript.

One of possible ways to process the pattern YYYYMMDD (4-digit year, 2-digit month, 2-digit day) is using RegExp:
use String.prototype.match() to match groups of digits of necessary length ((\d{n}))
destructure array of matches (skipping first item that holds the entire string) into variables yyyy, mm,dd
build up the desired output, using template string
Following is a quick demo:
const dateStr = '20200528',
[,yyyy,mm,dd] = dateStr.match(/(\d{4})(\d{2})(\d{2})/),
result = `${dd}/${mm}/${yyyy}`
console.log(result)
.as-console-wrapper{min-height:100%;}

Related

How to decode this time string [duplicate]

This question already has answers here:
Parsing a string to a date in JavaScript
(35 answers)
Converting a string formatted YYYYMMDDHHMMSS into a JavaScript Date object
(6 answers)
How to add 30 minutes to a JavaScript Date object?
(29 answers)
How do I format a date in JavaScript?
(68 answers)
Closed last year.
I got this string from an api
After looking at it I realized it was a dat/time
20220112201146
I then decoded it by hand to be
2022(Y)01(M)12(D)20(H)11(M)46(S)
How would I slice everything up to be Y:M:D:H:M:S?
Example:
2022:01:12:20:11:46
And then add 80 mins to it?
Extract the various parts (year, month, day, etc) via regex, transform it to ISO 8601 format, parse it to a Date instance, then add 80 minutes
const str = "20220112201146"
const rx = /(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/
const iso8601 = str.replace(rx, "$1-$2-$3T$4:$5:$6")
console.log("iso8601:", iso8601)
const date = new Date(iso8601)
console.log("original date:", date.toLocaleString())
date.setMinutes(date.getMinutes() + 80)
console.log("future date:", date.toLocaleString())

Convert Date without "T" and "Z" to Date with T-Z Format [duplicate]

This question already has answers here:
Parsing a string to a date in JavaScript
(35 answers)
How do I format a date in JavaScript?
(68 answers)
Closed 1 year ago.
I have a JSON file with date format in the form of
2021-09-21 15:37:29.590 +03:00.
How can we convert it to a Date in a T-Z Format ?
You can pass it into the Date constructor and call toISOString.
const convert = (dateString) => new Date(dateString).toISOString();
console.log(convert('2021-09-21 15:37:29.590 +03:00')); // 2021-09-21T12:37:29.590Z

Convert Numeric String to Readable Date Format in JavaScript [duplicate]

This question already has answers here:
Parsing a string to a date in JavaScript
(35 answers)
How do I format a date in JavaScript?
(68 answers)
Where can I find documentation on formatting a date in JavaScript?
(39 answers)
Closed 1 year ago.
Let's say I have a string 2021-08-13 and want to convert this to August 13, 2021. How would you achieve this as it's not a date object.
In my mind I can think of setting each numeric month to a text version of that month and re-arrange, however seeing if there are better ways of doing this.
Simple: convert the string into a Date object and use the toLocaleString function.
If you want to get rid of the timezone so the date stays the same wherever the user is you can first convert it into an ISO string, get rid of the 'Z' in the end, and then convert it back into the Date object.
const dateString = '2021-08-13'
const localeOptions = {dateStyle: 'long'}
const dateTimezone = new Date(dateString).toLocaleString('en-US', localeOptions)
const dateWithoutTimezone = new Date(new Date(dateString).toISOString().slice(0,-1)).toLocaleString('en-US', localeOptions)
console.log(dateTimezone)
console.log(dateWithoutTimezone)
Convert your string date to a JS Date Object
let strDate = "2021-08-13";
let date = new Date(strDate);
console.log(date.toDateString())
Learn more about Date object here: JavaScript Date

how to get date of my localtime like 2020-03-11T02:59 with javascript [duplicate]

This question already has answers here:
Where can I find documentation on formatting a date in JavaScript?
(39 answers)
How to format a Date in MM/dd/yyyy HH:mm:ss format in JavaScript? [duplicate]
(4 answers)
Closed 2 years ago.
i want to get my country date like this 2020-03-11T02:59 i try to JSON and toISOString but it give me the hours does not true
You can change the timezone with the offset at the end of the date string (e.g. -05:00).
And then create the date string you want using get functions.
You will need to pad the month, day, hour and minutes. However, the .get (eg .getMonth) functions return numbers, so you will also have to convert them to strings.
For example: event.getMonth().toString().padStart(2,0);
event is your date object.
.getMonth() returns the numeric value of the month of your date object
.toString() converts that number to a string value
.padStart(2,0) will add zeros to the front of the string if it is less than 2 characters.
// Set the date with timezone offset
let event = new Date("2020-03-11T02:59:00-08:00");
// Format your string
let newEvent = `${event.getFullYear()}-${event.getMonth().toString().padStart(2,0)}-${event.getDate().toString().padStart(2,0)}T${event.getHours().toString().padStart(2,0)}:${event.getMinutes().toString().padStart(2,0)}`;
console.log(newEvent);

Format date from DD.MM.YY format to DD/MM/YYYY format [duplicate]

This question already has answers here:
How do I format a date in JavaScript?
(68 answers)
Why does Date.parse give incorrect results?
(11 answers)
Closed 3 years ago.
How can I format date from DD.MM.YY format to any other format (preferably DD/MM/YYYY) with full 4 digits year value that I can input to new Date() by using javascript?
I find similar issue but in PHP code, is there an library that can convert that in javascript?
I'm getting Invalid Date error when inputting that date format to new Date()
Vanilla JavaScript can be used to convert the date string into a Date object by calling split(",") on the string, parseInt() on each returned array item, and then adding 1900 or 2000 to the year component based on your own needs. Date() takes the year, month, and day as arguments.
Once you have a JavaScript date object, a method like this using padStart() can output the date into the DD/MM/YYYY format.
See this example codepen: https://codepen.io/volleio/pen/BXWKyp

Categories

Resources