使用 JavaScript 指定日期之前最接近的星期日

发布于 2024-11-08 08:05:10 字数 418 浏览 0 评论 0原文

我需要知道 php 中给定日期的上周日的日期javascript

让我们有一个函数 Give_me_last_Sunday

give_me_last_Sunday('20110517') is 20110515
give_me_last_Sunday('20110604') is 20110529

完整备份在星期日=每周完成。如果我想恢复每日备份,我需要完整(每周)和每日备份。我需要在恢复到临时目录之前复制备份文件,因此我恢复每日备份时我需要知道需要沿着每日文件复制哪些每周备份文件。

我的想法是获取给定日期的朱利安表示法(或类似的东西),然后减去 1 并检查是否是星期日......不确定这是否是最好的主意以及如何将给定日期转换为我可以减去的东西。

I need to know the date for last Sunday for given date in php & javascript

Let's have a function give_me_last_Sunday

give_me_last_Sunday('20110517') is 20110515
give_me_last_Sunday('20110604') is 20110529

The full backup is done on Sundays = weekly. If I want to restore daily backup I need full (weekly) and daily backup. I need to copy backup files before restoring to temp directory so I restoring daily backup I need to know what weekly backup file I need to copy along the daily file.

My thought was to get Julian representation (or something similar) for the given date and then subtract 1 and check if it is Sunday ... Not sure if this is the best idea and how to convert given date into something I can subtract.

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

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

发布评论

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

评论(4

世界等同你 2024-11-15 08:05:10

基于 Thomas 的努力,并且假设输入字符串正是您指定的格式,那么:

function lastSunday(d) {
  var d = d.replace(/(^\d{4})(\d{2})(\d{2}$)/,'$1/$2/$3');
  d = new Date(d);
  d.setDate(d.getDate() - d.getDay());
  return d;
}

编辑

如果我现在要这样写,我不会依赖 Date 对象来解析字符串,而是自己做:

function lastSunday(s) {
  var d = new Date(s.substring(0,4), s.substring(4,6) - 1, s.substring(6));
  d.setDate(d.getDate() - d.getDay());
  return d;
}

虽然格式 yyyy /mm/dd 被我测试过的所有浏览器正确解析,我认为坚持基本方法更稳健。特别是当它们可能更有效率时。

Based on Thomas' effort, and provided the input string is exactly the format you specified, then:

function lastSunday(d) {
  var d = d.replace(/(^\d{4})(\d{2})(\d{2}$)/,'$1/$2/$3');
  d = new Date(d);
  d.setDate(d.getDate() - d.getDay());
  return d;
}

Edit

If I were to write that now, I'd not depend on the Date object parsing the string but do it myself:

function lastSunday(s) {
  var d = new Date(s.substring(0,4), s.substring(4,6) - 1, s.substring(6));
  d.setDate(d.getDate() - d.getDay());
  return d;
}

While the format yyyy/mm/dd is parsed correctly by all browsers I've tested, I think it's more robust to stick to basic methods. Particularly when they are likely more efficient.

蓝色星空 2024-11-15 08:05:10

好的,这仅适用于 JavaScript。您有一个输入,需要从中提取月份、日期和年份。以下只是关于如何获取日期的部分答案:

<script type="text/javascript">
var myDate=new Date();
myDate.setFullYear(2011,4,16)

var a = myDate.getDate();
var t = myDate.getDay();
var r = a - t;
document.write("The date last Sunday was "  + r);

</script>

因此 setFullYear 函数将 myDate 设置为指定的日期,其中前四位数字是年份,接下来是月份(0 = 一月,1 = 二月)。 ,...)。最后一个是实际日期。然后上面的代码给出了之前周日的日期。我猜您可以添加更多代码来获取月份(使用 getMonth() 方法)。以下是一些可能有用的链接

(您可能可以找到您需要的其他功能)

我希望这会有所帮助,尽管它不是一个完整的答案。

Ok so this is for JavaScript only. You have an input that you need to extract the month, date, and year from. The following is just partly an answer then on how to get the date:

<script type="text/javascript">
var myDate=new Date();
myDate.setFullYear(2011,4,16)

var a = myDate.getDate();
var t = myDate.getDay();
var r = a - t;
document.write("The date last Sunday was "  + r);

</script>

So the setFullYear function sets the myDate to the date specified where the first four digits is the year, the next are is the month (0= Jan, 1= Feb.,...). The last one is the actually date. Then the above code gives you the date of the Sunday before that. I am guessing that you can add more code to get the month (use getMonth() method). Here are a few links that might be helpful

(You can probably find the other functions that you need)

I hope this helps a bit even though it is not a complete answer.

眼眸印温柔 2024-11-15 08:05:10

是的,strtotime 已被移植到 JS,例如 http://phpjs.org/functions/strtotime:554这里。

Yup and strtotime has been ported to JS for eg http://phpjs.org/functions/strtotime:554 here.

[浮城] 2024-11-15 08:05:10

最终代码(非常感谢@Thomas & @Rob)

function lastSunday(d) {
  var d = d.replace(/(^\d{4})(\d{2})(\d{2}$)/,'$1/$2/$3');

  d = new Date(d);
  d.setDate(d.getDate() - d.getDay());

  year = d.getFullYear()+'';
  month = d.getMonth()+1+'';
  day = d.getDate()+'';
  if ( month.length == 1 ) month = "0" + month; // Add leading zeros to month and date if required
  if ( day.length == 1 ) day = "0" + day;  

  return year+month+day;
}

final code (big thanks to @Thomas & @Rob)

function lastSunday(d) {
  var d = d.replace(/(^\d{4})(\d{2})(\d{2}$)/,'$1/$2/$3');

  d = new Date(d);
  d.setDate(d.getDate() - d.getDay());

  year = d.getFullYear()+'';
  month = d.getMonth()+1+'';
  day = d.getDate()+'';
  if ( month.length == 1 ) month = "0" + month; // Add leading zeros to month and date if required
  if ( day.length == 1 ) day = "0" + day;  

  return year+month+day;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文