1

I have one input type text:

 <input type="text" id="policyholder-dob" name="policyholder-dob" />

I want to type number in this field in mm/dd/yyyy format: like 01/01/2014 This is my js code but its not working, what mistake have I made?

function dateFormatter(date) {
    var formattedDate = date.getDate() 
        + '/' + (date.getMonth() + 1) + '/' + date.getFullYear();
    return formattedDate;
}

var nextduedate = $("#policyholder-dob").val();

var dateFormatDate = nextduedate.slice(0, 2);
var dateFormatMonth = nextduedate.slice(2, 4);
var dateFormatYear = nextduedate.slice(4, 8);
var totalFormat = dateFormatMonth + '/' + dateFormatDate + '/' + dateFormatYear;
var againNewDate = new Date(totalFormat);
againNewDate.setDate(againNewDate.getDate() + 1);
var todaydate = dateFormatter(againNewDate);

$("#policyholder-dob").prop("value", todaydate);

Any help will be really appreciated.

Panda
  • 1,231
  • 18
  • 27
supersaiyan
  • 1,670
  • 5
  • 16
  • 36

2 Answers2

0

Thankfully, your input is consistently in this format:

mm/dd/yyyy

So you can convert it to a Date object through a custom function, such as:

function stringToDate(str){
    var date = str.split("/"),
        m = date[0],
        d = date[1],
        y = date[2],
        temp = [];
    temp.push(y,m,d);
    return (new Date(temp.join("-"))).toUTCString();
}

Or:

function stringToDate(str){
    var date = str.split("/"),
        m = date[0],
        d = date[1],
        y = date[2];
    return (new Date(y + "-" + m + "-" + d)).toUTCString();
}

Etc..

Calling it is easy:

stringToDate("12/27/1963");

And it will return the correct timestamp in GMT (so that your local timezone won't affect the date (EST -5, causing it to be 26th)):

Fri, 27 Dec 1963 00:00:00 GMT //Late december

Example

There are various ways to accomplish this, this is one of them.

Charlie
  • 11,380
  • 19
  • 83
  • 138
  • This is not cross-browser (requires ECMA5 for ISO8601), and is prone to error due to browser differences with string parsing. – Xotic750 Mar 03 '14 at 03:10
  • @Xotic750 As I said, there are various ways to do it, this is one of them. What are the browser differences with string parsing though? – Charlie Mar 03 '14 at 05:11
  • It was just a note to readers. With very little effort you can make this cross-browser and at the same time avoid string parsing pitfalls. There is a great deal of information here on SO about string parsing reliability, I don't intend to repeat it. – Xotic750 Mar 03 '14 at 05:31
0

I'd suggest moment.js for date manipulation. You're going to run into a world of hurt if you're trying to add 1 to month. What happens when the month is December and you end up with 13 as your month. Let a library handle all of that headache for you. And you can create your moment date with the string that you pull from the val. You substrings or parsing.

var d = moment('01/31/2014');                 // creates a date of Jan 31st, 2014
var duration = moment.duration({'days' : 1}); // creates a duration object for 1 day
d.add(duration);                              // add duration to date
alert(d.format('MM/DD/YYYY'));                // alerts 02/01/2014

Here's a fiddle showing it off.

VtoCorleone
  • 16,813
  • 5
  • 37
  • 51