How to decode this time string [duplicate] - javascript

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())

Related

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

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

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%;}

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);

How to convert dd/mm/yyyy in ISO format in react native [duplicate]

This question already has answers here:
Convert dd-mm-yyyy string to date
(15 answers)
Closed 3 years ago.
I am getting date in this format 01/01/2022 (dd/mm/yyyy). And i have to convert it into ISO format("2022-01-01T00:00:00.000+01:00"). I've tried but I get invalid time value error.
const newDate ='01/01/2022'
"2022-01-01T00:00:00.000+01:00"
try this
var str = "25/09/2019";
darr = str.split("/"); // ["25", "09", "2019"]
var ISOFormat = new Date(parseInt(darr[2]),parseInt(darr[1])-1,parseInt(darr[0]));
console.log(ISOFormat.toISOString());
also refer this link by mozilla

Categories

Resources