Reputation: 560
I am using the Javascript function toLocaleString() to convert date object into string (As it's output is very user friendly), for storage purpose, and then I want to fetch that saved data (obviously in the form of string) later, and I want to compare such to dates.
I have date stored in this format : February 5, 2016 7:04:40 PM IST
But That's not possible directly, so I tried to convert that string to date object using both implicit (using Date constructor) and explicit (Using Date Object) methods. But it gives Invalid date, as output, when printed. Then I google a lot to find out how can I convert date from this format directly back to the Date Object so that I can perform the desired date operations. But I couldn't found anything worth using. Please help me to came out with a solution to that problem, so that I can complete my project.
Upvotes: 4
Views: 7277
Reputation: 147363
I am using the Javascript function toLocaleString() to convert date object into string (As it's output is very user friendly), for storage purpose
That is not a good idea. You should store data in the best format for storage and present to users in the best format for humans. The two are often different.
and then I want to fetch that saved data (obviously in the form of string) later, and I want to compare such to dates.
Far better to store the date in a format that your database supports natively, or as an ISO 8601 compliant string. There is no standard for time zone abbreviations, but I'll guess that IST is India Standard Time, which is +0530. The equivalent UTC ISO 8601 string is "2016-02-05T13:34:40Z", which can be created from your initial Date object using toISOString.
Then, when you read from the database, convert to whatever format is required for use.
Most ECMAScript hosts will correctly parse "2016-02-06T13:34:40Z", however some will not. A simple parser that also validates the values is:
function parseISO(s) {
var b = s.split(/\D/);
var d = new Date(Date.UTC(b[0],--b[1],b[2],b[3],b[4],b[5],(b[6]?(b[6]+'000').slice(0,3):0)))
return d && d.getUTCMonth() == b[1] && d.getUTCHours() == b[3] &&
d.getUTCMinutes() == b[4]? d : new Date(NaN);
}
document.write(parseISO('2016-02-05T13:34:40Z').toLocaleString());
Upvotes: 2
Reputation: 689
If you really need to use pure javascript you should create a function which parses your string in order to use those arguments to create a date object.
var months = {
'january': 0,
'february': 1,
'march': 2,
'april': 3,
'may': 4,
'june': 5,
'july': 6,
'august': 7,
'september': 8,
'october': 9,
'november': 10,
'december': 11,
};
var strDate = "February 5, 2016 7:04:40 PM IST";
var parts = strDate.split(' ');
var year = parts[2];
var month = months[parts[0].toLowerCase()];
var day = parts[1].replace(/\,/g,"");
var timeParts = parts[3].split(':');
var hours = timeParts[0];
var minutes = timeParts[1];
var seconds = timeParts[2];
if (parts[4]==="PM") {
hours = parseInt(hours) + 12;
}
var d = new Date(year, month, day, hours, minutes, seconds);
alert(d.toLocaleString());
In the futur please put a little more efforts into it. You could've easily written this yourself.
edit 2 : Actually none of this was needed. You can use the Date.parse() function as long if you remove the IST from the end.
var strDate = "February 5, 2016 7:04:40 PM";
var d = new Date("February 5, 2016 7:04:40 PM");
alert(d.toLocaleString());
Then if you want you can set the timezone manually.
Upvotes: 2