周数到月 [英] Week number to month

查看:142
本文介绍了周数到月的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

鉴于我有一个特定年份的周数,如何获得本周开始的月份名称? (使用Obj-c)



例如2009年第2周应转换为2009年1月
,2004年第51周转换为2004年12月

解决方案

找到答案有问题:获得一周的第一天

  //转换周到月
int weekNum = sdi.mWeek;
const char * months [13] = {1月,二月,三月,四月,五月,六月,七月,八月,九月,十月,十一月,十二月,十一月};
struct tm curYear = {0,0,0,1,0,sdi.mYear,0,0,0};
time_t secsSinceEpoch = mktime(& curyear)+ weekNum * 86400 * 7;
struct tm * candidateDate = gmtime(& secsSinceEpoch);
if(candidateDate-> tm_wday!= 1)
{
secsSinceEpoch + =(86400 *(candidateDate-> tm_wday-1));
candidateDate = gmtime(& secsSinceEpoch);
}
// [months [candidateDate-> tm_mon]]返回月份


Given that I have the week number of a particular year, how do I get the month's name in which the week starts? (using Obj-c)

e.g. Week 2 of 2009 should convert to January 2009 and Week 51 of 2004 should convert to December 2004

解决方案

Found answer at question: get first day of given week

//convert week to month
    int weekNum = sdi.mWeek;
    const char *months[13]={"January","February","March","April","May","June","July","August","September","October","November","December","January"};
    struct tm curYear={0,0,0,1,0,sdi.mYear,0,0,0}; 
    time_t secsSinceEpoch=mktime(&curYear)+weekNum*86400*7;
    struct tm *candidateDate=gmtime(&secsSinceEpoch);    
    if (candidateDate->tm_wday!=1)
    {
        secsSinceEpoch+=(86400*(candidateDate->tm_wday-1)); 
        candidateDate=gmtime(&secsSinceEpoch);
    }    
    // [months[candidateDate->tm_mon]] returns month

这篇关于周数到月的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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