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

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

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

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

Invalid Date for some UTC Strings Javascript [duplicate]

This question already has answers here:
Why does Date.parse give incorrect results?
(11 answers)
Closed 3 years ago.
Trying to get UTC day of the week for any given timestamp on any given machine (w/ their own local time) I used:
var date = new Date(timestamp).toLocaleString('en-GB', { timeZone: 'UTC' });
Once I try to convert the date string to UTC date I get Invalid Date for some dates... it all seems pretty weird.
$ node
> date = new Date('15/08/2019, 00:00:00');
Invalid Date
> date = new Date('12/08/2019, 00:00:00');
2019-12-08T00:00:00.000Z
> date = new Date('15/08/2019');
Any idea where the Invalid Date issue may come from?
By converting the timestamps to strings using the "en-GB" locale, it looks like you're getting them in DD/MM/YYYY format. But in your second example, the strings are being interpreted as "MM/DD/YYYY" in whatever your default locale is, so the first call fails because 15 isn't a valid month number.

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

How can I convert this date format? [duplicate]

This question already has answers here:
Convert date to specific format in javascript?
(4 answers)
Closed 5 years ago.
An API I use provides a date in the object like 2018-02-14T17:00:00. How can I convert this to make it say: Tuesday, February 14th 7:00 pm
I know how to use .getMonth() methods on a date object but is it possible to do something similar with a string in a date format like this in Javascript?
You can use momentjs to format the date object.
console.log(new moment('2018-02-14T17:00:00').format('dddd, MMMM Do h:mm a'))
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.20.1/moment.js"></script>
You can parse the string into separated values first using String.split() method.
let rawDate = '2018-02-14T17:00:00';
let date = rawDate.split("T")[0]; //2018-02-14
let time = rawDate.split("T")[1]; //17:00:00
let year = date.split("-")[0],
month = date.split("-")[1],
day = date.split("-")[2];
let hr = time.split(":")[0],
mm = time.split(":")[1],
ss = time.split(":")[2];
Now just format these separated values using new Date(year, month, day) etc.
try this
console.log(new moment('2018-02-14T17:00:00').format('LLLL'))
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.20.1/moment.js"></script>
JavaScript natively does not provide a way for you to stringify a date with a provided format.
To do that, you can use moment.js. Here's the specific documentation that says how to do that:
https://momentjs.com/docs/#/displaying/format/

Categories

Resources