gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 16:02:49 24 4
gpt4 key购买 nike

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>

  • 当用户输入这样的日期时:12/25/2016 它将被转换成这种格式:Mon Dec 25 2016。我在文本框中显示了使用 javascript 的功能,但目前没有发生任何转换。我那里有 JSON,但不知道如何使用它。

展开代码段以修复文本框。

最佳答案

只需与 input 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

使用 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>

关于javascript - 将用户输入的日期转换为 JSON mm/dd/yyyy 格式并将其显示为结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40669880/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com