This question already has answers here:
How do I format a date in JavaScript?
(68 answers)
Closed last month.
I have data available in ISO format which is 2021-06-01T00:00:00.000Z. I want to convert it into 1st June 2021 format.
Below is what I did
const dat = 2021-06-06T00:00:00.000Z;
const d = new Date(dat);
console.log(d.toDateString());result
I am getting this Tue Jun 01 2021. How can I format this into desired format?
You can use moment.js libary for date conversion.
let yourDate = "2021-06-06T00:00:00.000Z";
let convertedDate = moment(yourDate).format("Do MMMM YYYY");
console.log("Result (6th June 2021) ==>>", convertedDate)
<script type = "text/JavaScript" src = "https://MomentJS.com/downloads/moment.js"></script>
Related
This question already has answers here:
Parsing a string to a date in JavaScript
(35 answers)
Closed 10 months ago.
I need to convert string "Apr 28 2022 12:00AM" to date. I have tried Date.parse, but it is returning "Nan".
var dtstring = "Apr 28 2022 12:00AM";
var dt = Date.parse(dtstring);
console.log(dt);
On the MDN documentation page it is said:
The ECMAScript specification states: If the String does not conform to
the standard format the function may fall back to any
implementation–specific heuristics or implementation–specific parsing
algorithm. Unrecognizable strings or dates containing illegal element
values in ISO formatted strings shall cause Date.parse() to return
NaN.
So the date format is as follows
if we correct and write the code is working
var dtstring = "Apr 28 2022 12:00";
var dt = Date.parse(dtstring);
console.log(dt);
If you can change the string of the date, you could just use the Date constructor: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/Date
So for example:
new Date('2022-04-28T12:00:00');
But if you can't change the string, you'll have to use a javascript library that can parse this date.
Here is an example with date-fns and it's function parse:
parse('Apr 28 2022 12:00 AM', 'MMM dd yyyy p', new Date());
Here is the link of the documentation: https://date-fns.org/v2.28.0/docs/parse
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 2 years ago.
Is there a way to convert this date input 01 Nov 2020, 9:55pm +10:00 to a timestamp like this 2020-11-01T11:55:00Z?
use new Date("01 Nov 2020, 9:55 pm +10:00").toISOString()
NOTE: make sure that 9:55 and PM are separated
const date = new Date("01 Nov 2020, 9:55 pm +10:00").toISOString()
console.log(date)
you can use moment lib.
sample:
const moment = require("moment");
const inputDate = "01 Nov 2020, 9:55pm +10:00";
const formatedDate = moment(inputDate, "DD MMM YYYY, HH:mmA ZZ");
This question already has answers here:
Why does Date.parse give incorrect results?
(11 answers)
Closed 5 years ago.
How do I create a date from a non-English language?
When using an English formatted string, the date returns a valid date.
var value = "3 mei, 2017 - 11:36";
//var format = 'd MMMM, yyyy - hh:mm';
var d = new Date(value);
When using a non-English formatted string, the date returns an invalid date.
var value = "3 may, 2017 - 11:36";
//var format = 'd MMMM, yyyy - hh:mm';
var d = new Date(value);
What should I change to my code to make the second version run?
I prefer you to use moment.js library.
https://momentjs.com/
This question already has answers here:
Get String in YYYYMMDD format from JS date object?
(53 answers)
Closed 6 years ago.
I'm trying to get the first and last day of the month with the format 2016.02.29, but I'm not sure how to turn the date into this format.
The following code (taken from here):
var date = new Date();
var firstDay = new Date(date.getFullYear(), date.getMonth(), 1);
var lastDay = new Date(date.getFullYear(), date.getMonth() + 1, 0);
Gives this format: Mon Feb 29 2016 00:00:00 GMT+0100 (W. Europe Standard Time).
How do i make it look like 2016.02.29? I could manipulate the string to get the result I want, but isn't there a way to get it by just defining a format?
You can use the MomentJs to format any date by specifying the exact format.
Load the momentjs or momentjs + locale script in your page
moment().format('YYYY.MM.DD');
This question already has answers here:
Convert iso timestamp to date format with Javascript?
(4 answers)
Closed 7 years ago.
I have date in ISO-format like:
2016-02-17T16:40:30
How can I convert it to a human-readable date, for example:
17 Feb 2016 16:40
First of all, you need to create a date using your original date string.
var d = new Date('2016-02-17T16:40:30');
And then you can use this to fetch a readable date format:
d.toDateString();
Will return:
Wed Feb 17 2016