我正在更新旧的应用程序,我在其中接受日期(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日(拼写的月份)。 这可能吗?
答案 0 :(得分:0)
您需要解析日期值才能执行此操作:
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);