如何以 dd/mmm/yyyy 格式将日期作为文本保存到 Websql?
How to save a Date as text to Websql in format dd/mmm/yyyy?
我正在更新一个旧应用程序,我在其中输入日期 (DOB) 并(当前)将其作为文本保存到 websql 数据库中。我知道 websql 不再真正使用了,但此时我不想重新做整个应用程序。
例如
db.transaction(function(tx) {
tx.executeSql("CREATE TABLE IF NOT EXISTS personal(ID INTEGER PRIMARY KEY ASC, name TEXT, surname TEXT, added_on DATETIME, note TEXT, dob TEXT, gender TEXT, )", []);
和
var dob = document.getElementById("dob").value;
目前将 DOB 保存为 dd/mm/yy,这使得美国用户很难将日期显示为 05/06/10。
我希望它显示为 2010 年 6 月 5 日(拼写的月份)。
这可能吗?
您需要解析日期值才能执行此操作:
var value = "05/06/2010".split("/"); //please note that you'll need to use four digits as a year for years in 2000. Two digit years refer to years from 1900 till 1999.
//Split the date into parts using the /.
//Use a month array to display the correct month as text
var months = ["January","February","March","April","May","June","July","August","September", "October","November", "December"];
//Feed the date as year, month-1, day else we will get ackward results.
var date = new Date(value[2], value[1]-1, value[0]); //subtract 1 from the month since it's zero indexed.
document.body.innerHTML += date.getDate() + " " + months[date.getMonth()] + " " + date.getFullYear(); //rewrite the string
//In modern browsers (IE11, firefox and Chrome) you can use toLocaleString() option
document.body.innerHTML += "<br />";
var options = { year: 'numeric', month: 'long', day: 'numeric' };
document.body.innerHTML += date.toLocaleString('en-UK',options);
我正在更新一个旧应用程序,我在其中输入日期 (DOB) 并(当前)将其作为文本保存到 websql 数据库中。我知道 websql 不再真正使用了,但此时我不想重新做整个应用程序。 例如
db.transaction(function(tx) {
tx.executeSql("CREATE TABLE IF NOT EXISTS personal(ID INTEGER PRIMARY KEY ASC, name TEXT, surname TEXT, added_on DATETIME, note TEXT, dob TEXT, gender TEXT, )", []);
和
var dob = document.getElementById("dob").value;
目前将 DOB 保存为 dd/mm/yy,这使得美国用户很难将日期显示为 05/06/10。 我希望它显示为 2010 年 6 月 5 日(拼写的月份)。 这可能吗?
您需要解析日期值才能执行此操作:
var value = "05/06/2010".split("/"); //please note that you'll need to use four digits as a year for years in 2000. Two digit years refer to years from 1900 till 1999.
//Split the date into parts using the /.
//Use a month array to display the correct month as text
var months = ["January","February","March","April","May","June","July","August","September", "October","November", "December"];
//Feed the date as year, month-1, day else we will get ackward results.
var date = new Date(value[2], value[1]-1, value[0]); //subtract 1 from the month since it's zero indexed.
document.body.innerHTML += date.getDate() + " " + months[date.getMonth()] + " " + date.getFullYear(); //rewrite the string
//In modern browsers (IE11, firefox and Chrome) you can use toLocaleString() option
document.body.innerHTML += "<br />";
var options = { year: 'numeric', month: 'long', day: 'numeric' };
document.body.innerHTML += date.toLocaleString('en-UK',options);