This question already has answers here:
How do I format a date in JavaScript?
(68 answers)
Closed 4 years ago.
Given a date string in yyyy-MM-dd format like 2018-08-09, how would I convert it to yyyy/MM/dd format (2018/08/09) and dd/MM/yyyy format (09/08/2018) in javascript?
I have seen How to format a JavaScript date but it is about how to format a javascript date while i want to format a javascript date string
To replace all the hyphens in the date String with slashes, you can split the String on "-" and join it on "/".
var dateStr = "2018-08-09";
var newDateStr = dateStr.split('-').join('/');//output is 2018/08/09
console.log(newDateStr);
To change a date String from yyyy-MM-dd format to dd/MM/yyyy format, you can split the String on "-", reverse it, and join it on "/".
var dateStr = "2018-08-09";
var newDateStr = dateStr.split('-').reverse().join('/');//output is 09/08/2018
console.log(newDateStr);
Related
This question already has answers here:
How do I format a date in JavaScript?
(68 answers)
Parsing a string to a date in JavaScript
(35 answers)
Closed 1 year ago.
I have a dateformat
date="11032020"(mmddyyyy)
output expected:11/03/2020
I want to convert this to mm/dd/yyyy in nodejs.I am a begineer to nodejs.
I saw many posts where they convert from mm-dd-yyyy to different format or related.I tried converting to ISO but that did not work out.
I am not able to convert mmddyyyy to different format.
Could you please help me out in this.
If you're certain that the input is MMDDYYYY and don't need to validate it, you could use a regular expression:
let format_date = input => input.replace(/(\d{2})(\d{2})(\d{4})/, "$1/$2/$3")
format_date("11032020") // "11/03/2020"
With moment.js you can do:
const date = "11032020"
const res = moment(date, "MMDDYYYY").format('MM/DD/YYYY')
console.log(res)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js" integrity="sha512-qTXRIMyZIFb8iQcfjXWCO8+M5Tbc38Qi5WzdPOYZHIlZpzBHG3L3by84BBBOiRGiEb7KKtAOAs5qYdUiZiQNNQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
This question already has answers here:
Parsing a string to a date in JavaScript
(35 answers)
Converting String in ISO8601 T-Z format to Date
(3 answers)
Convert string to datetime
(12 answers)
Closed 1 year ago.
How can I convert date string to date object in javascript in yyyy-mm-dd format i.e 2021-06-10
let date = '2021-06-10' //typeof == string
or
let date = 2021-06-10T00:00:00.000Z // I want only 2021-06-10 of type date and not string
the typeof must give me date type instead of string. I have tried toString(), toLocaleString() etc etc, all these gives me dates in string , but I have to set the value in form control so this must be of date type.
Any help!!
This question already has answers here:
How do I split a string with multiple separators in JavaScript?
(25 answers)
Closed 2 years ago.
I have CSV with dates in format DD-MM-YYYY but sometimes I work in Excel and it changes the dates to format DD/MM/YYYY
Code example
var dateParts = value['TAX_CALCULATION_DATE'].split("/"); //id date has "-" or "/"
var dateObject = new Date(+dateParts[2], dateParts[1] - 1, +dateParts[0]);
firestoreObject.TAX_CALCULATION_DATE = firebase.firestore.Timestamp.fromDate(dateObject);
How to include "/" and "-" in one split?
You can use regex in split;
// Matches / or -
const regex = /\/|-/g;
yourDate.split(regex);
This question already has answers here:
Convert dd-mm-yyyy string to date
(15 answers)
Parsing a string to a date in JavaScript
(35 answers)
Closed 2 years ago.
Trying converting the value vr 27.03.2020 to a Date object
<script>
let date = new Date("vr 27.03.2020");
console.log(date);
</script>
Result:
Invalid Date
What is the best way convert the value to a Date object?
PS: vr is abbreviation from dutch vrijdag (=friday)
This question already has answers here:
Convert dd-mm-yyyy string to date
(15 answers)
how to parse a "dd/mm/yyyy" or "dd-mm-yyyy" or "dd-mmm-yyyy" formatted date string using JavaScript or jQuery [duplicate]
(5 answers)
Closed 8 years ago.
I have string as 06/08/2013 that I want to convert into Date Object
I do
var transactionDate = Date.parse(transactionDateAsString);
and I get NaN
How can I tell javascript to format the string as dd/mm/yyyy?
Break it down and spoon-feed it:
var parts = transactionDateAsString.split("/");
var date = new Date(parts[2],parts[1]-1,parts[0]);
Date.parse() from the docs:
Parses a string representation of a date, and returns the number of milliseconds since January 1, 1970, 00:00:00 UTC.
You probably want to use the Date constructor:
var transactionDate = new Date(transactionDateAsString);