我编写了获取日期的代码,但该日期(变量)仅在同一函数中定义,但我需要将其作为全局变量。我需要做什么?有一部分代码,
<script type = "text/javascript" >
var year = "";
var month = "";
var day = ""
var hour = "";
var minut = "";
var time = "";
function myFunction() {
year = document.getElementById("id1").value;
month = document.getElementById("id2").value;
day = document.getElementById("id3").value;
hour = document.getElementById("id4").value;
minut = document.getElementById("id5").value;
var d = new Date();
d.setHours(hour);
d.setMinutes(minut);
d.setDate(day);
d.setMonth(month - 1);
d.setFullYear(year);
var now = new Date();
today = now.toISOString();
var time = d.toISOString();
}
var resource = {
"summary": "Sample Event testing " + Math.floor((Math.random() * 10) + 1),
"start": {
"dateTime": //set date for this place
},
"end": {
"dateTime": twoHoursLater
}
};
</script>
这是代码的一部分。 我需要设置时间为&#34;&#34; dateTime&#34;://为这个地方设置日期&#34;这个地方。
答案 0 :(得分:0)
使MyFunction返回您计算的日期值。从你的对象中调用MyFunction方法
function GetDateTime(){
// get the date time based on your requirement
year =document.getElementById("id1").value;
month =document.getElementById("id2").value;
day =document.getElementById("id3").value;
hour =document.getElementById("id4").value;
minut =document.getElementById("id5").value;
var d = new Date();
d.setHours(hour);
d.setMinutes(minut);
d.setDate(day);
d.setMonth(month-1);
d.setFullYear(year);
var now = new Date();
today = now.toISOString();
var time= d.toISOString();
return "DateValue"
}
来自全局变量的执行此操作
var obj = {
summary : "Summary",
Start: { dateTime: GetDateTime()},
End:{dateTime: {GetDateTime()}
}