计算本周的第一天和最后几天 [英] Calculating first and last days of current week

查看:147
本文介绍了计算本周的第一天和最后几天的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

参考预先回答的问题:获取第一个和最后一天当前周

Reference to pre-answered question at: Getting first and last days of current week

上述链接中有两个答案。其中一个是理论上的,另一个是被称为PyObjC(Python-Objective C bridge language)的语言,而一个快速的谷歌搜索证实了PyObjC不能与iPhone SDK一起使用。

There are two answers in the above link. One of them is theoretical and the other is in a language called as PyObjC (Python-Objective C bridge language), and a quick google search confirms that PyObjC does not work with iPhone SDK.

所以关于这个问题,如何将PyObjC代码翻译成与iPhone SDK兼容。

So regarding the question, how is it possible to get the PyObjC code translated to be compatible with iPhone SDK.

目标:假设今天(星期二)是19日,太阳。是17日(周的开始)和星期六23日是周末。我想要一个字符串,如19/01 - 23/01 [ie

Target: Supposing today (Tue.) is 19th, and Sun. was 17th (start of week) and Sat. 23rd is end of week. I want to get a string like 19/01 - 23/01 [i.e. The start of week (hypen) end of week]

推荐答案

如果你有一个 NSDate ,您可以使用当前的 NSCalendar 来检索该日期的 NSDateComponents 。将 NSDateComponents 工作日设置为1(本周的第一天),创建副本,并设置副本的工作日到7(一周的最后一天)。然后使用 NSCalendar NSDateComponents 转换回各自的 NSDate 对象,然后可以使用 NSDateFormatter 创建字符串表示。

If you have an NSDate, you can use the current NSCalendar to retrieve that date's NSDateComponents. Set the NSDateComponents's weekday to 1 (the first day of the week), create a copy, and set the copy's weekday to 7 (the last day of the week). Then use the NSCalendar to convert both the NSDateComponents back to their respective NSDate objects, after which you can use an NSDateFormatter to create your string representation.

此链接具有关于如何获取日期工作日的示例: http://stackoverflow.com/questions/1057349#1057405

This link has an example about how to get a date's weekday: http://stackoverflow.com/questions/1057349#1057405

这篇关于计算本周的第一天和最后几天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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