简体   繁体   English

如何将使用javascript中的toLocaleString()转换的字符串转换回Date对象?

[英]How can I convert a string converted using toLocaleString() in javascript back to Date Object?

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. 我正在使用Javascript函数toLocaleString()将日期对象转换为字符串(因为它的输出非常用户友好),用于存储目的,然后我想稍后获取保存的数据(显然是以字符串的形式),我想要将其与日期进行比较。

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. 但这不可能直接实现,因此我尝试使用隐式(使用Date构造函数)和显式(使用Date对象)方法将该字符串转换为date对象。 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. 然后,我在Google上进行了大量搜索,以了解如何将日期从这种格式直接转换回Date对象,以便执行所需的日期操作。 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. 请帮助我提出该问题的解决方案,以便我可以完成我的项目。

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. 如果您确实需要使用纯JavaScript,则应创建一个函数来解析您的字符串,以便使用这些参数创建日期对象。

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. 编辑2:实际上,这些都不需要。 You can use the Date.parse() function as long if you remove the IST from the end. 只要从末尾删除IST,就可以使用Date.parse()函数。

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 我正在使用Javascript函数toLocaleString()将日期对象转换为字符串(因为其输出非常用户友好),用于存储

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. 最好用数据库本身支持的格式或与ISO 8601兼容的字符串存储日期。 There is no standard for time zone abbreviations, but I'll guess that IST is India Standard Time, which is +0530. 时区缩写没有标准,但我猜IST是印度标准时间,即+0530。 The equivalent UTC ISO 8601 string is "2016-02-05T13:34:40Z", which can be created from your initial Date object using toISOString . 等效的UTC ISO 8601字符串为“ 2016-02-05T13:34:40Z”,可以使用toISOString从初始Date对象创建该字符串。

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. 大多数ECMAScript主机将正确解析“ 2016-02-06T13:34:40Z”,但某些主机则不能。 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()); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将此字符串转换为javascript Date对象? - How can I convert this string to a javascript Date object? 在 JavaScript 中,有没有办法在不使用 toLocaleString 的情况下将日期转换为时区? - In JavaScript, is there a way to convert a Date to a timezone without using toLocaleString? 如何将javascript中的日期转换为字符串 - how can i convert a date in javascript to string 如何在 Javascript 中将字符串转换为 object? - How can I convert a string to object in Javascript? 如何根据语言环境(与 .toLocaleString 相反)将字符串转换为数字? - How do I convert String to Number according to locale (opposite of .toLocaleString)? 如何将 HH:mm:ss 字符串转换为 JavaScript 日期对象? - How can I convert a HH:mm:ss string to a JavaScript Date object? 如何将转换后的字符串转换回数组? - How to convert a converted string back into an array? Javascript日期未使用`toLocaleString`正确格式化 - Javascript Date is not formatting correctly using `toLocaleString` 如何在Chrome和Firefox中将此字符串转换为Javascript日期? - How can I convert this string to a Javascript date in both Chrome and Firefox? 如何在JavaScript中将字符串日期数字转换为datetime? - How can I convert string date number to datetime in JavaScript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM