简体   繁体   中英

How to change into desired Date Format using jquery?

I'm working on asp.net project, I want to change the format of string date using jquery. I'm getting the formatted date like "07-MAY-99" but I want to date like "07/05/1999". how can I do this using jquery, can anyone help me ?

I usually use momentjs inmy projects. To me, it's really good.

Try this method... as it is simple and custom created code.. I hope it works for you...

 var date = '07-MAY-99'; date = date.split('-'); var months = ['JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL', 'AUG', 'SEP', 'OCT', 'NOV', 'DEC']; var newDate =''; $.each(months, function(key,value){ if(value == date[1]) { var m = key+1; newDate = m+'/'+date['0']+'/'+date['2']; } }); date = new Date(newDate) alert(("0" + date.getDate()).slice(-2) + '/' + ("0" + (date.getMonth() + 1)).slice(-2) + '/' + date.getFullYear()); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 

You can use date.js to achieve this:

var date = new Date('07-MAY-99');
var newDate = date.toString('dd-MM-yy');

You can use click [here] ( http://www.datejs.com/ ) to achieve this:

var date = new Date('2014-01-06');
var newDate = date.toString('dd/MM/yy');

Simply:)

Try to reconstruct DateTime string as like this:

HTML:

<input type="text" id="yourDatepickerId"/>   <!--Here is your DateTime picker-->

jQuery:

 var dateObj = $("#yourDatepickerId").val();  //<--Here is you get DateTime  value from picker

 var newDate = new Date(dateObj);
 var formattedString = [newDate.Date(),newDate.Month()+1,newDate.getFullYear()].join("/");

 alert(formattedString ); //Result : d/mm/yyyy

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM