生成两个日期内的随机日期数组的优雅方法

发布于 2024-12-29 21:53:15 字数 1311 浏览 0 评论 0原文

我有一个日期选择器,其中显示两个月,我想在每个可见月份中随机选择 3 个日期

  $('.date').datepicker({
    minDate: new Date(),
    dateFormat: 'DD, MM, d, yy',
    constrainInput: true,
    beforeShowDay: processDates,
    numberOfMonths: 2,
    showButtonPanel: true,
    showOn: "button",
    buttonImage: "images/calendar_icon.jpg",
    buttonImageOnly: true    
  });

这是我的计算

var now = new Date();
var nowTime = parseInt(now.getTime()/1000);
var randomDateSet = {};

function getRandomSet(y,m) {
  var monthIndex = "m"+y+""+m; // m20121 for Jan
  if (randomDateSet[monthIndex]) return randomDateSet[monthIndex];
  // generate here
.
. - I need this part
.
  return randomDateSet[monthIndex];
}

function processDay(date) { // this is calculated for each day so we need a singleton for the array
  var dateTime = parseInt(date.getTime()/1000);
  if (dateTime <= (nowTime-86400)) {
    return [false]; // earlier than today
  }
  var m = date.getMonth(), d = date.getDate(), y = date.getFullYear();
  var randomDates = getRandomSet(y,m);

  for (i = 0; i < randomDates.length; i++) {
    if($.inArray((m+1) + '-' + d + '-' + y,randomDates) != -1 || new Date() > date) {
      return [true,"highlight","Some message"];
    }
  }
  return [true,"normal"]; // ordinary day
}

I have a datepicker where I show two months and I want to randomly choose 3 dates in each visible month

  $('.date').datepicker({
    minDate: new Date(),
    dateFormat: 'DD, MM, d, yy',
    constrainInput: true,
    beforeShowDay: processDates,
    numberOfMonths: 2,
    showButtonPanel: true,
    showOn: "button",
    buttonImage: "images/calendar_icon.jpg",
    buttonImageOnly: true    
  });

Here is my calculation

var now = new Date();
var nowTime = parseInt(now.getTime()/1000);
var randomDateSet = {};

function getRandomSet(y,m) {
  var monthIndex = "m"+y+""+m; // m20121 for Jan
  if (randomDateSet[monthIndex]) return randomDateSet[monthIndex];
  // generate here
.
. - I need this part
.
  return randomDateSet[monthIndex];
}

function processDay(date) { // this is calculated for each day so we need a singleton for the array
  var dateTime = parseInt(date.getTime()/1000);
  if (dateTime <= (nowTime-86400)) {
    return [false]; // earlier than today
  }
  var m = date.getMonth(), d = date.getDate(), y = date.getFullYear();
  var randomDates = getRandomSet(y,m);

  for (i = 0; i < randomDates.length; i++) {
    if($.inArray((m+1) + '-' + d + '-' + y,randomDates) != -1 || new Date() > date) {
      return [true,"highlight","Some message"];
    }
  }
  return [true,"normal"]; // ordinary day
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

a√萤火虫的光℡ 2025-01-05 21:53:15

也许我错过了一些东西,但这不是吗?

function randomDate(start, end) {
  return new Date(start.getTime() + Math.random() * (end.getTime() - start.getTime()));
}

const d = randomDate(new Date(2012, 0, 1), new Date());
console.log(d);

Maybe I am missing something, but isn't this it?

function randomDate(start, end) {
  return new Date(start.getTime() + Math.random() * (end.getTime() - start.getTime()));
}

const d = randomDate(new Date(2012, 0, 1), new Date());
console.log(d);

半葬歌 2025-01-05 21:53:15
console.log(
  new Date(new Date() - Math.random()*(1e+12))
)

console.log(
  new Date(new Date() - Math.random()*(1e+12))
)

可爱暴击 2025-01-05 21:53:15

使用Moment.js && @Demven Weir 的答案,获取“03/02/1975”等字符串值。

moment(new Date(+(new Date()) - Math.floor(Math.random()*10000000000)))
.format('MM/DD/YYYY');

注意:不断地每次添加零以增加产生的年份跨度。

Using Moment.js && @Demven Weir's answer to get a string value like "03/02/1975".

moment(new Date(+(new Date()) - Math.floor(Math.random()*10000000000)))
.format('MM/DD/YYYY');

NOTE: Keep adding a zero at a time to increase the span of years produced.

向地狱狂奔 2025-01-05 21:53:15
function generateRandomDate() {
return new Date(+(new Date()) - Math.floor(Math.random() * 10000000000));
}

(new generateRandomDate()).toLocaleDateString('en-US')

您将获得美国格式的随机日期 4/25/2021

请参阅此演示 jsfiddle

function generateRandomDate() {
return new Date(+(new Date()) - Math.floor(Math.random() * 10000000000));
}

(new generateRandomDate()).toLocaleDateString('en-US')

You will get a random date in US format 4/25/2021

See this Demo on jsfiddle

夏花。依旧 2025-01-05 21:53:15

您可以将边界日期转换为整数 (Date.getTime()),然后使用 Math.random() 在给定边界内生成随机日期。然后使用 Date.setTime() 返回到 Date 对象。

You can convert the boundary dates to integers (Date.getTime()) and then use Math.random() to generate your random dates within given boundaries. Then go back to Date objects with Date.setTime().

∝单色的世界 2025-01-05 21:53:15

您可以使用此片段来获取随机日期和格式;

const emptyDate = new Date();
const randomDate = new Date();
const dateFormatter = Intl.DateTimeFormat('sv-SE');
const formattedRandomDate = dateFormatter.format(emptyDate.setDate(randomDate.getDate() - Math.floor(Math.random()*1000)));

You can use this snippet to get random date and format;

const emptyDate = new Date();
const randomDate = new Date();
const dateFormatter = Intl.DateTimeFormat('sv-SE');
const formattedRandomDate = dateFormatter.format(emptyDate.setDate(randomDate.getDate() - Math.floor(Math.random()*1000)));
紫竹語嫣☆ 2025-01-05 21:53:15

来自 date-fns 中的示例

const result = eachDayOfInterval({
  start: new Date(2014, 9, 6),
  end: new Date(2014, 9, 10)
})

From examples in date-fns

const result = eachDayOfInterval({
  start: new Date(2014, 9, 6),
  end: new Date(2014, 9, 10)
})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文