0

How to get date 03-Aug-2006 in this format?

$(document).ready(function(){
$('#btn').click(function(){
var val=$('#aa').val();
alert(formatDate(val))
})
});
function formatDate(inputStr) {
    var date = new Date(inputStr);
    return date
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="date" id="aa">
<button id="btn">
click me
</button>

If my date format is 2017-05-04T14:30:46.06 this means,then can I get dd-mmm-yyyy format?

Currently am using this function

function formatDate(inputStr) {
    var date = new Date(inputStr);
    var a = inputStr.split("T")[0];
    var datePart = a.match(/\d+/g),
    year = datePart[0].substring(0),
    month = datePart[1],
    day = datePart[2];
    return day + '-' + month + '-' + year;
}

But here I am unable to get dd-mmm-yyyy format.

krish
  • 1,077
  • 6
  • 24
  • 49
  • do you mind if you use javascript? because I found it easier to explain it – Alberto Martínez May 04 '17 at 09:37
  • You can use moment.js it will give you full flexibity over formatting of datete – Agam Banga May 04 '17 at 09:37
  • no I dont want to use moment.js – krish May 04 '17 at 09:39
  • Maybe [this previous post](http://stackoverflow.com/questions/3552461/how-to-format-a-javascript-date) can help you! – Douwe de Haan May 04 '17 at 09:39
  • @krish any reason why not? It'll solve the problem for you in moments (pun intended). Otherwise, you can build your own formatter out of the date functions (https://www.w3schools.com/jsref/jsref_obj_date.asp) - you'll have to provide your own list of month name strings etc though – ADyson May 04 '17 at 09:40
  • @ADyson Have a look at the comments (and especially the size noted) from [this answer](http://stackoverflow.com/a/3552493/1336174). – Douwe de Haan May 04 '17 at 09:41
  • @DouwedeHaan 11.6kb is really not that scary in the modern world. And then when the OP has more requirements for dates in the future, or has to support other formats, do calculations etc etc etc, as will almost inevitably happen (maybe not in this project, but in another one) they will know how to use a library. IMHO there is no point spending time re-inventing the wheel. Ok so for this specific requirement there's a specific solution already available in that answer, but it doesn't provide any flexibility for the future. A good coder should always be anticipating the next requirement... – ADyson May 04 '17 at 09:43
  • @DouwedeHaan ... - once you a show a user something good, they always want to add to it. – ADyson May 04 '17 at 09:44
  • @krish: please check if my answer is what you need – Rahul Arora May 04 '17 at 09:49

1 Answers1

1

You can try this:

var a = "2017-05-04T14:30:46.06";

var months = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];

var formatDate = function(date){
    var mydate = new Date(date);
    mydate = ("0"+mydate.getDay()).slice(-2) +'-'+ months[mydate.getMonth()] + "-" + mydate.getFullYear(); 
   //the slice part is to add a leading zero in case of a single digit number
    return mydate;
}

console.log(formatDate(a));
Rahul Arora
  • 4,503
  • 1
  • 16
  • 24