Convert Dd/mm/yyyy To Mm/dd/yyyy In Javascript
I want to convert dd/mm/yyyy to mm/dd/yyyy in javascript.
Solution 1:
var initial='dd/mm/yyyy'.split(/\//);
console.log( [ initial[1], initial[0], initial[2] ].join('/')); //=>'mm/dd/yyyy'
Edit 2021/05/14: A snippet using ES20xx
constpad = v => v.padStart(2, `0`);
const initialDate= newDate().toLocaleDateString("nl-NL")
.split(/[-/]/).map(pad).join("/");
consttoFragments = dateString => initialDate
.split(/[-/]/).map(pad);
constdateTo_mmddyyyy = ([date, month, year], divider = "/") =>
`${month}${divider}${date}${divider}${year}`;
const [date, month, year] = toFragments(initialDate);
console.log( `initial (dd/mm/yyyy): ${initialDate}`);
console.log( `reformatted to mm/dd/yyyy (array join): ${
[month, date, year].join('/') }` );
console.log( `reformatted to mm-dd-yyyy (function): ${
dateTo_mmddyyyy(toFragments(initialDate), "-") }` );
Solution 2:
var date = "24/09/1977";
var datearray = date.split("/");
var newdate = datearray[1] + '/' + datearray[0] + '/' + datearray[2];
newdate will contain 09/24/1977. The split method will split the string wherever it finds a "/", so if the date string was "24/9/1977", it would still work and return 9/24/1977.
Solution 3:
This is a moment.js version. The library can be found at http://momentjs.com/
//specify the date string and the format it's initially invar mydate = moment('15/11/2000', 'DD/MM/YYYY');
//format that date into a different format
moment(mydate).format("MM/DD/YYYY");
//outputs 11/15/2000
Solution 4:
Convert dd/mon/yyyy
to mm/dd/yyyy
:
months = {'jan': '01', 'feb': '02', 'mar': '03', 'apr': '04', 'may': '05',
'jun': '06', 'jul': '07', 'aug': '08', 'sep': '09', 'oct': '10', 'nov': '11',
'dec': '12'};
split = 'dd/mon/yyyy'.split('/');
[months[split[1]], split[0], split[2]].join('/');
Convert dd/mm/yyyy
to mm/dd/yyyy
:
split = 'dd/mm/yyyy'.split('/');
[split[1], split[0], split[2]].join('/');
Solution 5:
var months = newArray("jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec");
var ddSplit = '12/23/2011'.split('/'); //date is mm/dd/yyyy formatalert(ddSplit[1]+'-'+months[ddSplit[0]-1] + '-' + ddSplit[2])
Post a Comment for "Convert Dd/mm/yyyy To Mm/dd/yyyy In Javascript"