【问题标题】:Transform day in year to date转换年初至今的日期
【发布时间】:2014-11-02 11:53:26
【问题描述】:

如果我只知道日期是数字,我如何找到今年的日期...

假设我知道那一天是 '1' 那么得到 01.01.2014,如果我知道那一天是 '32' 那么得到 01.02.2014?

在javascript中可以吗?

php呢?

【问题讨论】:

  • 所有支持日期的语言都有可能。
  • 一切皆有可能。但你应该决定,你想要客户端(javascript)或服务器端(php)处理日期......请参阅php中的mktime()函数......

标签: javascript php jquery date date-format


【解决方案1】:

PHP方式:

$first_day_of_this_year = strtotime( date( 'Y-01-01 00:00:00' ) ); //as unix timestamp
$after_32_days = $first_day_of_this_year + 32 * 24 * 60 * 60;
echo date( "Y-m-d", $after_32_days );

这将输出2014-02-02

这将始终适用于当年。如果您想在其他年份使用它,只需将第一个 date() 函数中的 Y 替换为所需年份即可。

这应该适用于闰年。

编辑:

我做了一个函数:

function day_number_to_date( $day_in_year, $year = null ) {
    $year = ( is_null( $year ) ) ? date("Y") : $year; //use current year if it was not passed to function
    $first_day_of_year = strtotime( date( "$year-01-01 00:00:00" ) ); //first day of year as unix timestamp
    $days_to_add = $day_in_year - 1;
    $target_timestamp = $first_day_of_year + $days_to_add * 24 * 60 * 60;
    $target_date = date( "Y-m-d", $target_timestamp );
    return $target_date;
}
echo day_number_to_date( 32 ); //2014-02-01
echo day_number_to_date( 32, 2020 ); //2020-02-01
echo day_number_to_date( 400 ); //2015-02-04

【讨论】:

  • 为什么这对我不起作用 - 给我错误的日期:$target_date = date("l, Y-d-m", $target_timestamp);
  • @JamesD。它对我有用(输出Saturday, 2014-01-02),除了你用月换日。
【解决方案2】:

在 JavaScript 中,您可以通过简单地创建一个新的 Date 对象来做到这一点,并将 days 参数设置为您需要的一年中的哪一天 - 请参阅 MDN 上的参数注释部分:

var dayInYear = 32;
var newDate = new Date(2014, 0, dayInYear);
// newDate is 01 Feb.

或者如果你有一个现有的 Date 对象:

var theDate = new Date('01/01/2014');
var dayInYear = 32;
var newDate = new Date(theDate.getFullYear(), theDate.getMonth(), dayInYear);

【讨论】:

    【解决方案3】:

    阅读您的问题后,您似乎想为函数提供日期和年份以获取特定日期。

    function getDateFromDay( year, day) {
         return new Date((new Date(year, 0)).setDate(day));
    }
    
    getDateFromDay(2014, 1); // will give Wed Jan 01 2014 00:00:00
    

    【讨论】:

      【解决方案4】:

      试试这个:

      var date = new Date("" + new Date().getFullYear() );
      var day = 32;
      
      date.setDate(date.getDate() + day-1);
      
      console.log(date); // => Sat Feb 01 2014 ...
      

      【讨论】:

        【解决方案5】:

        function day2Date( day, year ) {
          return new Date(year,0,day);
        }
        console.log( day2Date( 32, 2014 ) ); //gives Sat Feb 01 2014 00:00:00

        【讨论】:

          【解决方案6】:

          您还需要您的代码来知道它是哪一年——讨厌的每四年 2 月 29 日——但这只是连续减去月份长度直到余数小于下个月长度的问题(同时跟踪上次减去哪个月份)。像这样的片段(伪C):

          day_to_month (year, day_in_year)
            {
            day_count = day_in_year;
            if (not_leap_year());
              while (day_in_year < month [month_count])
                {
                  subtract month[month_count++];
                }
            else
              while (day_in_year < leap_month [month_count])
                {
                   subtract leap_month [month_count++];
                }
              }
            date_set (year, month_count, day_count);
          

          我不会编写 javascript,但我不知道为什么即使在 bash 脚本中也无法做到这一点——只需要能够声明和初始化数组,以及基本的算术和流控制功能。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2018-04-25
            • 2016-11-08
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2019-06-07
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多