2015-04-18 33 views
1

我正在更新一个旧应用程序,我在日期(DOB)和(当前)将其作为文本保存到websql数据库中。我知道websql不是真的被使用了,但在这个时候我不想重新做整个应用程序。 例如如何以格式dd/mmm/yyyy将日期保存为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这使得难以为美国的用户为一个日期可以显示为10年5月6日。 我想把它显示为2010年6月5日(拼写出来的月份)。 这可能吗?

回答

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);

+0

我发现这... http://www.sqlite.org/lang_datefunc.html 这是否意味着我可以这样做: dob DATETIME(dob,'unixepoch','localtime') – user2110655

相关问题