javascript - Acrobat Javascript 日期

标签 javascript acrobat

我有一个以 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);

提前致谢!

最佳答案

我对 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));
}

关于javascript - Acrobat Javascript 日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12925166/

相关文章:

pdf - 如何确认 TrueType PDF 字体缺少字形

acrobat - 如何使用 Adob​​e Acrobat Pro 将批量(即巨大的 pdf 文件)转换为文本?

javascript - 如何从输入类型="file"多个中删除一、两个文件?

javascript - 表单 ID 重复

带有扩展面板行的 ASP.net 2.0 Gridview -- 如何构建面板 "on the fly"

Javascript navigator.mediaDevices.getUserMedia 将源移动到前面

c# - 在指定目的地以编程方式打开 pdf 文件

javascript - 根据日期/亚马逊槽类型从 API 返回数据

javascript - 尝试使用 flattenPages() 拼合 PDF 但没有任何反应

javascript - 将 Acrobat JavaScript 转换为 Adob​​e ES4 Deigner JavaScript