How to Convert Date Format from Dd.Mm.Yyyy to Dd/Mm/Yyyy in JavaScript

Convert DD-MM-YYYY to YYYY-MM-DD format using Javascript

This should do the magic

var date = "03-11-2014";
var newdate = date.split("-").reverse().join("-");

Javascript function to convert date yyyy/mm/dd to dd/mm/yy

If you're sure that the date that comes from the server is valid, a simple RegExp can help you to change the format:

function formatDate (input) {
var datePart = input.match(/\d+/g),
year = datePart[0].substring(2), // get only two digits
month = datePart[1], day = datePart[2];

return day+'/'+month+'/'+year;
}

formatDate ('2010/01/18'); // "18/01/10"

convert date from dd/mm/yyyy to yyyy-mm-dd in javascript

Do a simple split and join

var date = "03/05/2013";
var newdate = date.split("/").reverse().join("-");

"2013-05-03"

How to convert dd/mm/yyyy string into JavaScript Date object?

MM/DD/YYYY format

If you have the MM/DD/YYYY format which is default for JavaScript, you can simply pass your string to Date(string) constructor. It will parse it for you.

var dateString = "10/23/2015"; // Oct 23
var dateObject = new Date(dateString);
document.body.innerHTML = dateObject.toString();

Javascript convert date format from "dd/mm/yy" to "mm/dd/yyyy"

You can split the value by a / to get the month, date and year.

var s = $('#date1').val().split('/')
$('#date2').val(`${s[1]}/${s[0]}/20${s[2]}`)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input id="date1" type="text" value="25/12/21" >
<input id="date2" type="text" value="12/25/2021" >

javascript format date from DDMMYYYY to DD/MM/YYYY

If that's the exact format you're looking at, then you could just parse it out:

http://jsfiddle.net/q3yrtu0z/

$('#input1').change(function() {
$(this).val($(this).val().replace(/^(\d{2})(\d{2})(\d{4})$/, '$1/$2/$3'));
});

This is designed such that if the value is exactly 8 digits, then it will format it XX/XX/XXXX.

You may want to do additional validation on the validity of the date format (although you'd have to do this for MM/DD/YYYY inputs as well anyway)



Related Topics



Leave a reply



Submit