1

I have a function that takes an input and converts it to a date. I would like to get the date in the format January 01, 2014. The #date comes in with the form as January 10 - January 25. I need to split these into two different dates (the start date and the end date). The #year comes in as 2014

Not very experienced with JavaScript but trying to get this to work.

Here is my script:

$(function () {
    $('#submit').click(function (event) {
        event.preventDefault();
        var startDates = $('#date').val().split(" - ");
                    var year = $('#year').val();
                    var yearDec = parseInt(year, 10) + 1;
                    var payPdStart = startDates[0] + ' ' + year;
                    var payPdEnd = startDates[1] + ' ' + yearDec;
                    var startDate = Date.parse(payPdStart);
                    myStartDates = new Date(startDate);

                    var endDate = Date.parse(payPdEnd);
                    myEndDates = new Date(endDate);        })
})

The script outputs something like... Thu Dec 25 2014 00:00:00 GMT-0500 (Eastern Standard Time) I want it to show Thursday Dec 25, 2014 ( I don't need any time portion)

3 Answers3

2

You could

nyx
  • 477
  • 1
  • 4
  • 10
1

This should work for what you are doing with the Moment.js library

<script>
        $(function () {
            $('#submit').click(function (event) {
                event.preventDefault();
                var startDates = $('#date').val().split(" - ");
                var year = $('#year').val();
                var payPdStart = startDates[0] + ' '+ year;
                var payPdEnd = startDates[1] + ' ' + year;
                var startDate = Date.parse(payPdStart);
                myStartDates = moment(new Date(startDate)).format('MMMM DD, YYYY');
                var endDate = Date.parse(payPdEnd);
                myEndDates = moment(new Date(endDate)).format('MMMM DD, YYYY');
            })
        })
    </script>
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
tgbrunet
  • 260
  • 3
  • 11
0

"July, 03 2014"

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

Number.prototype.pad = function() {
    return (this < 10) ? ("0" + this) : this;
}
var d = new Date(),
    h = monthNames[d.getMonth()] + " " + d.getDay().pad() + ", " + d.getFullYear();
user2226755
  • 12,494
  • 5
  • 50
  • 73