我有一个文本字段,以mmmm d,yyyy(称为“到期日期”)显示日期,我正在尝试制作三个较小的字段,仅显示日期(d),月份(m)和每年(yyyy)在每个领域。
我尝试使用以下代码将数据导入每个字段:
var sField = 'Expiry Date'
然后我会根据需要将其自定义格式化为“d”,“m”或“yyyy”。在小格式预览窗口中,它将显示所需的输出,但字段仍为空白。
奇怪的是它只适用于以月份开头的格式。
我获得第一个日期的字段是从另一个计算创建的,如果它使它有任何不同。 “到期日期”从名为“日期”的字段中获取数据。以下是在“日期”值
之后30天分配到期日的代码// define the value for the date field
var sField = 'Date'
// define the format of the date string
var cFormatDate = 'mm/dd/yyyy';
// define some time constants
var fSecond = 1000; // number of milliseconds in one second
var fMinute = 60 * fSecond; // number of milliseconds in a minute
var fHour = 60 * fMinute; // number of milliseconds in an hour
var fDay = 24 * fHour; //number of milliseconds in a day
// get the field object's string value
var fTodayDate = this.getField(sField).value;
// convert the string value into a date object
var oDate = util.scand(cFormatDate, fTodayDate);
// convert the date object to a value in milliseconds
var fDate = oDate.getTime();
// add 30 days to value using the number of milliseconds in a day
var fNewDate = fDate + (30 * fDay);
// convert computed date value to date object
var oNewDate = new Date(fNewDate);
// set the field's value to the date string for the date object
event.value = util.printd(cFormatDate, oNewDate);
提前致谢!!
答案 0 :(得分:0)
我对Acrobat一无所知,但假设其Date对象符合ECMA-262。到目前为止,将日期字符串转换为日期对象的最佳方法是自己解析它,不要将它留给Date
函数/构造函数或Date.parse
。
在您的帖子中,日期字符串似乎与October 17, 2012
相似。下面有一个函数来帮助解决这个问题。
添加整天的最佳方法是将它们添加到日期,因此给定日期对象:
// Create a new Date object
var now = new Date();
// Copy it
var then = new Date(now);
// Add 30 days
then.setDate(then.getDate() + 30);
请注意,在1月28日(或闰年1月29日)之后添加30个日期将在3月结束。
日期字符串解析函数:
// Expects mmm d, yyyy e.g. October 17, 2012 or Oct 17, 2012
function parseDateString(s) {
var months={jan:0, feb:1, mar:2, apr:3, may:4, jun:5,
jul:6, aug:7, sep:8, oct:9, nov:10, dec:11};
var d = s.split(/\s/);
return new Date(d[2], months[d[0].toLowerCase().substring(0,3)], parseInt(d[1],10));
}
答案 1 :(得分:0)
我可能过度简化了一些事情,但是如果你有一个日期字段(我们将其命名为“DATE1”),其中包含完整的日期。
你不能只复制那个字段3次,将“DATE1”分配给所有三个的名字。这将采用您在原始日期字段中键入的日期,并在其他三个字段中复制它。然后进入字段属性,确保所有4个框都分配了“日期”格式 - 然后在3个较小的框上,分别为它们分配“dd”,“mm”或“yy”的自定义日期选项?