简体   繁体   English

使用JSON mm / dd / yyyy格式转换用户输入的日期并将其显示为结果

[英]Convert a User Entered Date with JSON mm/dd/yyyy Format and Display It as a Result

 function eraseText() { document.getElementById("textbox").value = " "; } /* var dateString = "\\/Date(1334514600000)\\/".substr(6); var currentTime = new Date(parseInt(dateString )); var month = currentTime.getMonth() + 1; var day = currentTime.getDate(); var year = currentTime.getFullYear(); var date = day + "/" + month + "/" + year; */ var request; var date; function getUserRequest() { return document.getElementById("request_input").value; } function getUserDate() { return document.getElementById("date_input").value; } function display() { request = getUserRequest(); date = getUserDate(); document.getElementById("textbox").value = date + " - " + request; } 
 body { background-color:#f5f5dc; padding-left:100px; padding-right:100px; padding-bottom:100px; } form { outline-offset:12px; outline-color:red; outline-width:3px; outline-style: solid; background-color:#008080; } h2 { color:#00cc00; text-align:left; } p { color:blue; text-align:left; } textarea { margin-left:430px } #clearbutton { text-align:left; } #submit { text-align:left; } 
 <!DOCTYPE html> <html> <head> <html lang="en"> <title>Project 4</title> <link rel="stylesheet" type="text/css" href="project4css.css"> <script src="project4js.js"></script> </head> <meta charset="utf-8"> <form id='form' name="form1" method = 'post'> <h2>Change and Update Packages</h2> <textarea id="textbox" rows="10" cols="50"> </textarea> <p>Enter Your Request:</p> <input type="text" id="request_input" name="request"> <p>Request Date:</p> <input type="text" id="date_input" name="date"> <br> <br> <br> <input id ="submit" type = "button" onclick="display()" value='Add Request'/> <br> <br> <input id="clear_button" type="button" name="clearbutton" value="Clear Request" onclick="javascript:eraseText();"> </form> </body> </html> 

  • When a user enters a date like so: 12/25/2016 it will be converted into this type of format: Mon Dec 25 2016 . 当用户输入日期(例如12/25/2016)时,它将转换为以下格式: Mon Dec 25 2016 I have the functionality working with javascript being displayed in the textbox but no conversion is happening at the moment. 我的功能与正在文本框中显示的javascript一起使用,但目前没有任何转换。 I have the JSON there but don't know how to utilize it. 我在那里有JSON,但不知道如何利用它。

Expand the snippet to fix the textbox. 展开代码段以修复文本框。

Simply use with input type="date" and toUTCString() . 只需与输入type =“ date”toUTCString()一起使用

 function check(){ var dates = document.getElementById('date').value; console.log(new Date(dates).toUTCString()) } 
 <input type="date" id="date" onchange="check()">check me 

Update answer with Json 使用Json更新答案

 function eraseText() { document.getElementById("textbox").value = " "; } function display() { var request = document.getElementById("request_input").value; var dates = document.getElementById("date_input").value; var d=new Date(dates); var date_array= '{"test_day":"'+d.getDay()+'","test_date":"'+d.getDate()+'","test_month":"'+d.getMonth()+'", "test_year":"'+d.getFullYear()+'","test_utc":"'+d.toUTCString()+'"}'; var obj = JSON.parse(date_array); document.getElementById("textbox").value = obj.test_utc +'-'+request; console.log(obj) } 
 body { background-color:#f5f5dc; padding-left:100px; padding-right:100px; padding-bottom:100px; } form { outline-offset:12px; outline-color:red; outline-width:3px; outline-style: solid; background-color:#008080; } h2 { color:#00cc00; text-align:left; } p { color:blue; text-align:left; } textarea { margin-left:430px } #clearbutton { text-align:left; } #submit { text-align:left; } 
 <form id='form' name="form1" method = 'post'> <h2>Change and Update Packages</h2> <textarea id="textbox" rows="10" cols="50"></textarea> <p>Enter Your Request:</p> <input type="text" id="request_input" name="request"> <p>Request Date:</p> <input type="date" id="date_input" name="date"> <br> <br> <br> <input id ="submit" type = "button" onclick="display()" value='Add Request'/> <br> <br> <input id="clear_button" type="button" name="clearbutton" value="Clear Request" onclick="javascript:eraseText();"> </form> 

I cant comment this on the other answer but using <input type="date"> will not work with other browsers aside from Chrome and Firefox. 我无法在其他答案上对此发表评论,但使用<input type="date">不能与Chrome和Firefox之外的其他浏览器一起使用。

You can instead use Date.parse() 您可以改用Date.parse()

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM