2

Please view the fiddle here.

I have a text box that inputs a date (in the format 12/11/2015) directly into the 'msg' placeholder of a paragraph.

The date displays just fine. But, I'm trying to get it to display in the following format -

$("#get").click(function (e) {
    e.preventDefault();
    $('#msg').html( $('input:text').val());   
    dateFormat: 'DD, MM d'
});

So, inputting today's date, as 12/11/2015 would yield a result of Friday, December 11

...but it's not working.

Where am I going wrong?

2
  • Take a look at toLocaleString(), it can accomplish what you're looking for with the right options. See this SO answer for usage. Commented Dec 11, 2015 at 16:06
  • @MattK - A great resource. Thank you, sir! Commented Dec 11, 2015 at 16:24

4 Answers 4

3

You can use Javascript Date Methods to get the desired output format.. Here is the w3schools page for reference, and here is code which gets the output you want:

$("#get").click(function (e) {
    e.preventDefault(); 
    var d = new Date( $('input:text').val() );
    var days = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
        var day = days[d.getDay()];
    var months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
    var month = months[d.getMonth()];
    var date = d.getDate();
    var output = day+", "+month+" "+date;
     $('#msg').html( output ); 
});

JSFiddle

Sign up to request clarification or add additional context in comments.

Comments

1

There is no direct way to get the stringified version of the month name and day name from the native Date object. You need to keep arrays of Month names and day names and query from that.

$(function(){
    $("#get").click(function (e) {
       e.preventDefault();
       var fDate = getDateFormatted($('input:text').val());
       $('#msg').html(fDate);   
   });

});    

function getDateFormatted(dateString)
{
  date = new Date(dateString);
  var days = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'];

 var monthNames = ["January", "February", "March", "April", "May", "June", "July",
                   "August", "September", "October","November", "December"];

  var day=date.getDay();
  var dayName = days[day+1];
  var monthIndex = date.getMonth();
  var year = date.getFullYear();

 return dayName + ', ' + monthNames[monthIndex+1] + ' ' +  date.getDate();

}

Here is a working sample.

3 Comments

But, I want to display the output as (example) Friday, December 11, so would I just add another variable and list days of the week and then call it in the return string?
I saw your update on jsbin. It's interesting, but it's making sense.
My only question is why, when I enter today's date, 12/11/2015, it returns a date string of Friday, December 5
0

Try this

var date = $('#datepicker').datepicker({ dateFormat: 'dd-mm-yy' }).val();

1 Comment

I updated the question. I'm trying to get the output to display as DD, MM d
0

You have to add dateformat plugin for jquery. have a look at jQuery date formatting

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.