1

The date input I have is for e.g.

var startDate = "January 16, 2013"

From older posts I tried to dig into, I know that I might need to use date.js from http://www.javascripttoolbox.com/lib/date/documentation.php

So I had an extra line in my html to fire it up

<script src="http://prestigeswimming.com/wp-content/plugins/cred-frontend-editor/date.js" type="text/javascript"></script> 
<script type="text/javascript">
   var startDate = "January 16, 2013"
   alert(startDate.getMonthName());
</script>

Am I supposed to do this to get an alert box saying "January"?

2
  • Side note: Date.js hasn't been maintained in years, and has a couple of parsing bugs around midnight. You might look at Moment.js. Commented Jan 11, 2013 at 17:48
  • @EmersonF, if you are able to change your date format when inputting it, you should make sure you follow this specification: ecma-international.org/ecma-262/5.1/#sec-15.9.1.15 Commented Jan 11, 2013 at 18:08

3 Answers 3

2

Try this new version of the the line:

var startDate = Date.parseString( "January 16, 2013");

In order to use the library you have to make use of the Date() object.

parseString is a function on that object that returns a Date object.

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

2 Comments

@T.J.Crowder - That is not what this documentation page seemed to say, search for parseString there => javascripttoolbox.com/lib/date/documentation.php. I'll admit, I didn't test it.
@ Hogan: It must be a different DateJS library! (The name is generic enough, eh?)
0

If you are only dealing with US locale dates and need to parse arbitrary string inputs into dates, you could also check out Sherlock.js for parsing the string into a Date object. Then, to get the month name, simply manipulate in Javascript:

var Sherlocked = Sherlock.parse('January 16');
var month = Sherlocked.startDate.getMonth();
var months = ["January","February","March","April","May","June","July","August","September","October","November","December"];
alert(months[month]);

Sherlock.js won't parse the year though. If you need support for that, you could parse the year out yourself using Sherlock's preprocessor system.

Disclosure: I am the creator of Sherlock.js

Comments

-1

There's no need to use a library:

var startDate = "January 16, 2013";
var d = new Date(startDate);
var month = d.getMonth() + 1;
var day = d.getDate();
var year = d.getFullYear();

alert(month + "-" + day + "-" + year);

http://jsfiddle.net/mPBSf/

The following will convert your date format into a valid ECMAScript date format:

var startDate = "ApRiL 16, 2013";
var noCommas = startDate.replace(/\,/g,'');
var withDashes = noCommas.replace(/ /g,"-");

function monthsToNumbers(date)
{
  var months = ["january","february","march","april","may","june","july","august","september","october","november","december"];
  var year;

  date = date.toLowerCase();

  for(var i = 0; i < months.length; i++)
  {
    if(date.indexOf(months[i]) != -1)
    {
      if(i+1 < 10)
        date = date.replace(months[i],"0" + (i+1));
      else
        date = date.replace(months[i],i+1);
    }
  }

  year = date.match(/\d{4}/);
  date = date.replace(year, "");
  date = year + "-" + date;
  date = date.substring(0,date.length-1);

  return date;
}

var d = new Date(monthsToNumbers(withDashes));
var month = d.getMonth() + 1;
var day = d.getDate();
var year = d.getFullYear();

alert(d);

http://jsfiddle.net/kWzSv/1/

4 Comments

You're relying on the Date constructor being able to parse that date, but the date is not in a format documented by the standard. So it may work for you, with your particular browser's JavaScript engine, in your particular locale, but it's not something you can rely on.
@ Alex: Again: In your locale. Did you test it with a French locale? Vietnamese? Using undocumented features, when you have a choice, is just not a good idea.
Yeah, I think split(" ")[0] would be better than hoping the parser works.
Added a new function to try and convert his format into an ECMAScript valid date format.

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.