Skip to content Skip to sidebar Skip to footer

Javascript: Convert String With Full Month To Date Object

I have a string variable 'PublishDate' and then when I check the value using 'PublishDate.value' it returns a string like 'October 04, 2016 9:28 AM' I need to convert it to a date

Solution 1:

Use an array of months to get the numerical value, and then just parse it however you'd like

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

functionpad(n) { return n<10?'0'+n:n }

varPublishDate = {
	value : "October 04, 2016 9:28 AM"
}

var parts = PublishDate.value.split(/[\s\,\:]+/);
var year  = +parts[2];
var month = months.indexOf(parts[0]);
var day   = +parts[1];

var date  = newDate(year, month, day);                  // date objectvar str   = pad(month+1) + '/' + pad(day) + '/' +  year; // the string "10/04/2016"console.log(date);
console.log(str);
.as-console-wrapper {top : 0}

Solution 2:

First, convert that String date into a Date object using the Date constructor:

var d = newDate(PublishDate.value);

EDIT: Always consider the case that your format may not be supported by the browser. In the case of Firefox, the MDN states that it will support certain date formats:

The string should be in a format recognized by the Date.parse() method (IETF-compliant RFC 2822 timestamps and also a version of ISO8601).

IE's MSDN Javascript reference also says:

It first tries to parse a date string by using the ISO Date Format. If the date string is not in ISO format, JavaScript tries to parse the date by using other Other Date Formats.

Also, consider that the MDN itself discourages parsing String dates using the constructor or Date.parse(), even though it works for your specific case:

Note: parsing of date strings with the Date constructor (and Date.parse, they are equivalent) is strongly discouraged due to browser differences and inconsistencies.

If you need to support other varieties of date formats or a variety of browsers, you can also consider Momtent.js javascript library or do your own parsing with RegEx, like in @RobG's and @adeneo's answers.


Then, you need to extract the parts you want (month, day, year) and concatenate them in a string with the slash as a separator, like in @user3254198's answer or @Steeve Pitis's answer:

d.getDate() + '/' 
    + (d.getMonth() + 1) + '/' 
    +  d.getFullYear()

Remember that the PublishDate.value.getMonth() method returns a zero-based month numerical representation of the month, so if you want a 1-12 value, add a +1 to it.

Feel free to ask any further! Good luck.

Solution 3:

// Example PublishDate resultvarPublishDate = {
  value: 'October 04, 2016 9:28 AM'
};

// convert "date string" into "date object"var date = newDate(PublishDate.value);

// build required "date string"var dateShort = (date.getMonth() +1) + '/'
  + date.getDate() + '/'
  + date.getFullYear();

// console log to show resultconsole.log(dateShort);

Solution 4:

Parsing a string like "October 04, 2016 9:28 AM" using the Date constructor is entirely implementation dependent and may result in an incorrect date or invalid date, so either use a parser (there are many good ones available) or write a small function yourself to parse the string.

But if you just want to reformat it, then parsing to a Date isn't required at all, just reformat the string. E.g.

/* Reformat a string like October 04, 2016 9:28 AM to m/d/y format
** @param {string} s - string in format MMMM DD, YYYY h:mm ap
** @returns {string} reformatted date in MM/DD/YYYY format
*/functionreformatDate(s) {
  var months = {jan:'01',feb:'02',mar:'03',apr:'04', may:'05',jun:'06',
                jul:'07',aug:'08',sep:'09',oct:'10',nov:'11',dec:'12'};
  var b = s.match(/\w+/g) || [];
  return months[b[0].toLowerCase().substr(0,3)] + '/' + b[1] + '/' + b[2];
}

console.log(reformatDate('October 04, 2016 9:28 AM'));
                

Solution 5:

There is a lot of question like this on SO ... Try to search a little next time.

var date = new Date(PublishDate.value);
console.log((date.getDate() + '/' + (date.getMonth() + 1) + '/' +  date.getFullYear());

Post a Comment for "Javascript: Convert String With Full Month To Date Object"