查找给定日期的一年中的星期,以mm / dd / yyyy为单位 [英] Find week of a year given the date in mm/dd/yyyy

查看:96
本文介绍了查找给定日期的一年中的星期,以mm / dd / yyyy为单位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找到某个年份的日期。我有一堆文件需要分类到 week1-2012和 week34-2011之类的文件夹中。我尝试搜索,但是很多结果并没有真正的帮助,因为我当前使用的是Perl v5.6.1,版本太旧,无法下载任何模块。我还找到了此链接(如何计算周数数字给定日期?),但我想知道如何轻松地获得一年中的每一天。正在考虑获取月份,并添加适当的天数以找出一年中的某天。任何帮助,将不胜感激。

I am trying to find the week that a date falls in, in a certain year. I have a bunch of files that need to be sorted into folders like "week1-2012" and "week34-2011". I tried searching but a lot of the results aren't really helping because I am currently using perl v5.6.1, super old and I can't download any modules. I also found this link ( How do I calculate the week number given a date?) of interest but was wondering how I would go about getting the day of year and week easily. Was thinking of getting the month, and adding the appropriate amount of days to find out the day in the year. Any help would be appreciated. An example of the year format I am looking for is

//year 2012
S  M  T  W  R  F  S
            1  2  3    <-- week #1 
4  5  6  7  8  9 10    <-- week #2 //came from the link

//dec year 2011
S   M  T  W  T  F  S
27 28 29 31            <-- week #52 or 53, not to sure the actual week


推荐答案

您可以使用以下核心模块: POSIX Time :: Local

You can use core modules: POSIX and Time::Local

1。将您的日期解析为(秒,分钟,小时,mday,月,年)

1.parse your date to (sec, min, hour, mday, month, year)

2.将日期转换为秒(纪元)

2.convert your date to seconds (epoch)

3。使用strftime函数从当前日期获取星期数

3.use function strftime to get week from current date

use strict;
use Time::Local;
use POSIX qw(strftime);

my $date = '08/15/2012';
my ($month, $day, $year) = split '/', $date;

my $epoch = timelocal( 0, 0, 0, $day, $month - 1, $year - 1900 );
my $week  = strftime( "%U", localtime( $epoch ) );

printf "Date: %s № Week: %s\n", $date, $week;

输出

Date: 08/15/2012 № Week: 33

这篇关于查找给定日期的一年中的星期,以mm / dd / yyyy为单位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆