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 pare 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 plete my project.
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 pare 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 plete my project.
Share Improve this question edited Feb 5, 2016 at 13:49 Ruan Mendes 92.4k31 gold badges159 silver badges222 bronze badges asked Feb 5, 2016 at 13:47 SKGSKG 5602 gold badges5 silver badges18 bronze badges 5- I would remend moment.js to work with dates. It proved very efficient for me. – Tornike Shavishvili Commented Feb 5, 2016 at 13:50
- Why do you need a "very user friendly" format for storage? (If by "storage" you mean in a database isn't it a date column?) You only need a user friendly format for what is displayed to the user - the rest of the time use whatever is easiest to work with in your code. – nnnnnn Commented Feb 5, 2016 at 13:59
- Juan, Thanks for the suggestion, actually, I am using Google App Script for my work, and I don't think that there we can use any javascript library, If I am wrong please let me know ! – SKG Commented Feb 5, 2016 at 13:59
- Hi nnnnnn, actually I am storing the data in a Spreadsheet using Google App Script, so I want that to be readable by anyone because at any time, I need to go through that spreadsheet, I can easily read that. – SKG Commented Feb 5, 2016 at 14:03
- It doesn't make sense to store a date in a user friendly manner. You store the data that is needed to create a user friendly display. – Ruan Mendes Commented Feb 5, 2016 at 14:40
2 Answers
Reset to default 2If 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.
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 pare such to dates.
Far better to store the date in a format that your database supports natively, or as an ISO 8601 pliant 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());