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

I am trying to create a javascript function to output a date from my database in the format (yyyy-mm-dd) and display it on the page as (dd / mm / yy).

I would be grateful for any help.

Thanks.

PD: Let me know if you need more clarification.

+10
javascript function date
source share
7 answers

If you are sure that the date coming from the server is valid, a simple RegExp can help you 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" 
+18
source share

The easiest way to assume that you are not worried about a dynamic function:

 function reformatDate(dateStr) { dArr = dateStr.split("-"); // ex input "2010-01-18" return dArr[2]+ "/" +dArr[1]+ "/" +dArr[0].substring(2); //ex out: "18/01/10" } 
+15
source share

Use the getDateFromFormat () and formatDate () functions from this source: http://mattkruse.com/javascript/date/source.html
There are also examples

+2
source share

You can also look at using date.js:

http://www.datejs.com

For future use of your application, you may want to return the time in UTC and the format using JavaScript. This will allow you to support different formats for different countries (in the USA, we are most familiar with DD-MM-YYYY or an instance), as well as with time zones.

+1
source share

Use any of these js functions to convert the date yyyy / mm / dd to dd / mm / yy

Type 1

 function ChangeFormateDate(oldDate){ var p = dateString.split(/\D/g) return [p[2],p[1],p[0] ].join("/") } 

Type 2

 function ChangeFormateDate(oldDate) { return oldDate.toString().split("/").reverse().join("/"); } 

You can call these functions with:

 ChangeFormateDate("2018/12/17") //"17/12/2018" 
+1
source share

This is a simple case, but everyone uses string methods! This is a bit barbaric :-)

The Date object is fully configured for this and will help you much further when you master it. Your date does not have a time zone, so I suggest you enter UTC both at the entrance and at the exit. The en-GB language uses dd-mm, but you should keep in mind that English-speaking users are divided into the middle of the date format, and each half finds the other format completely confusing. You should really try to make your date number format adapt to your preferences, all the more so because it’s easy!

So...

 new Vue({ el: '#vueRoot', data: {kennedy: '1963-11-22'}, computed:{ kennedyDdmm(){ return new Date(this.kennedy + 'T00:00:00Z') .toLocaleDateString('en-GB',{timeZone:'UTC'}) }, kennedyAuto(){ return new Date(this.kennedy + 'T00:00:00Z') .toLocaleDateString([],{timeZone:'UTC'}) } } }) 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script> <div id='vueRoot'> <h1>in => {{kennedy}}</h1> <h1>dd-MM-yy => {{kennedyDdmm}}</h1> <h1>respect user prefs => {{kennedyAuto}}</h1> </div> 
0
source share

You can also use destructuring and template literals if you are sure that you will always get the date in YYYY-MM-DD .

 const changeDateFormatTo = date => { const [yy, mm, dd] = date.split(/-/g); return '${dd}/${mm}/${yy}'; }; const formattedDate = changeDateFormatTo("2019-08-14"); console.log('Formatted date is: ${formattedDate}'); 

0
source share

All Articles