首页 \ 问答 \ 如何从当前时间到每天晚上10点30分(30分钟)获得时间间隔(How to get time interval for every 30:00(30 minutes) from current time to 10:30 pm)

如何从当前时间到每天晚上10点30分(30分钟)获得时间间隔(How to get time interval for every 30:00(30 minutes) from current time to 10:30 pm)

需要帮助来显示每30分钟的时间间隔,假设current time is 11:45 am

时间间隔应为: 12:00 pm,12:30 pm,01:00 pm,01:30 pm,02:00 pm,02:30 pm......10:30 pm.

 NSString *time = @"10.30 pm";

     NSDate *date1;
        NSDate *date2;
        {
            NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
            [formatter setDateFormat:@"hh.mm a"];
            date1 = [formatter dateFromString:time];
            date2 = [formatter dateFromString:[formatter stringFromDate:[NSDate date]]];

        }
        NSTimeInterval interval = [date1 timeIntervalSinceDate: date2];//[date1 timeIntervalSince1970] - [date2 timeIntervalSince1970];
        int hour = interval / 3600;
        int minute = (int)interval % 3600 / 60;

        NSLog(@"%@ %dh %dm", interval<0?@"-":@"+", ABS(hour), ABS(minute));

此代码返回当前时间和给定时间的差异,我如何进一步处理。


Need help to show the time intervals for every 30 mins, suppose the current time is 11:45 am then

Time intervals should be : 12:00 pm,12:30 pm,01:00 pm,01:30 pm,02:00 pm,02:30 pm......10:30 pm.

 NSString *time = @"10.30 pm";

     NSDate *date1;
        NSDate *date2;
        {
            NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
            [formatter setDateFormat:@"hh.mm a"];
            date1 = [formatter dateFromString:time];
            date2 = [formatter dateFromString:[formatter stringFromDate:[NSDate date]]];

        }
        NSTimeInterval interval = [date1 timeIntervalSinceDate: date2];//[date1 timeIntervalSince1970] - [date2 timeIntervalSince1970];
        int hour = interval / 3600;
        int minute = (int)interval % 3600 / 60;

        NSLog(@"%@ %dh %dm", interval<0?@"-":@"+", ABS(hour), ABS(minute));

This code returns me difference of current time and the given time how can I proceed further.


原文:https://stackoverflow.com/questions/37519033
更新时间:2023-08-15 21:08

最满意答案

你的问题在于这段代码: a[i] = 2 << (i-1);

假设类型为int因为它在C ++的大多数编译器中都是32位。

您需要覆盖它。

a[i] = 2ULL << (i-1);

您需要特别注意与移位运算符一起使用的类型,移动更多的位,操作数的大小是未定义的行为,这里可能发生任何事情( 样本 ),因为忘记后缀需要更多的预防措施。


Your problem is in this code: a[i] = 2 << (i-1);

2 is assumed of type int because of that it's 32bits in most compilers of C++.

You need to override this with.

a[i] = 2ULL << (i-1);

You need to take special care with types used with shifting operator, shifting more bits that the size of the operand is Undefined Behavior and anything could happen here (sample), with literals more precaution are need because of the forgot suffix.

相关问答

更多
  • Long是Object形式, Integer是int的对象形式。 long使用64位 。 int使用32位,因此只能保存高达±20亿(-2 31到+ 31 31 -1)的数字。 你应该使用long和int ,除非你需要使用从Object继承的方法,比如hashcode 。 Java.util.collections方法通常使用boxed( Object -wrapped)版本,因为它们需要适用于任何Object ,而原始类型(如int或long )不是Object 。 另一个区别是long和int是值得传递 ...
  • 为了不复制任何人的努力,请查看此问题以及相同主题的更高级的问题和答案 。 dthorpe在这个问题上的答案也非常详细。 这里的 小麦首次评论值得作为初审。 In order not to replicate anybody's effort, please see this question and this more advanced question and answers of the same topic. dthorpe's answer in this one is highly detaile ...
  • long和long int是相同的。 那么long long而long long int 。 在这两种情况下, int都是可选的。 对于两组之间的差异,C ++标准规定了每个的最小范围, long long 至少等于long 。 标准的控制部分(C ++ 11,但是这已经存在了很长时间)是一个3.9.1 Fundamental types ,第2节(稍后一节给出了无符号积分类型的类似规则): 有五种标准符号整数类型:signed char,short int,int,long int和long long i ...
  • 你的问题在于这段代码: a[i] = 2 << (i-1); 假设类型为int因为它在C ++的大多数编译器中都是32位。 您需要覆盖它。 a[i] = 2ULL << (i-1); 您需要特别注意与移位运算符一起使用的类型,移动更多的位,操作数的大小是未定义的行为,这里可能发生任何事情( 样本 ),因为忘记后缀需要更多的预防措施。 Your problem is in this code: a[i] = 2 << (i-1); 2 is assumed of type int because of tha ...
  • 来自的旧C函数::abs接受并返回int ,它不能保存平台上的大值。 请改用 (C ++ 17及更高版本)或 std::abs 。 另外, using namespace std;摆脱它using namespace std; 并恰当地限定名称。 请参阅为什么“使用命名空间std”被视为不良做法? 完整代码: #include #include int main() { long long int a, ...
  • 他们是一样的。 因为long默认是int的修饰符,所以int可以省略。 short和short int , unsigned和unsigned int等都是一样的。 这里要理解的重要一点是, long , short和unsigned是类型修饰符 ,而不是类型本身,不像int , char , double等。 They are the same. Because long is a modifier for an int by default, int can be omitted. The same g ...
  • 是的,这是有效的,它包含在草案C ++ 11标准第7.1.6.2 简单类型说明符中 ,它说: 表10总结了simple-type-specifiers的有效组合及其指定的类型。 在表10 简单类型说明符及其指定的类型表示: long long “long long int” 和: short “short int” Yes, this is valid it is covered in the draft C++11 standard section 7.1.6.2 Simp ...
  • 当你调用std::strtoull(str.c_str(), NULL, 0); ,它提供的第一个参数相当于一个空字符串 ,因为string本质上是一个以null结尾的字符序列。 其次, std::strtoull()不会使用字节序列进行转换,它会根据字符串的字面意思进行转换。 即你会得到2048 std::strtoull("2048", NULL, 10) 。 另外需要注意的是, unsigned long long是一个64位的数据类型,而你的字节数组只提供32位。 您需要用零填充其他32个字节才能获 ...
  • 你不会 - 数组索引在Java中总是int值。 它不允许具有多于Integer.MAX_VALUE元素的数组。 数组的length由length字段表示,其类型为int 。 因此,无法创建长度大于Integer.MAX_VALUE的数组。 规范没有明确地将其解析出来,但您可以从涉及的类型中推断出来。 You wouldn't - array indexes are always int values in Java. It doesn't allow for an array with more than ...
  • 诸如long long类的基本积分类型具有它们可以表示的有限范围。 如果计算导致结果的结果大于可以表示的结果,则它将溢出。 有符号整数溢出在C ++中有未定义的行为。 通过使用整数数组来表示数字的不同部分,可以表示无限范围的数字(理论上,内存限制了实践)。 该技术称为任意精度算术 。 C ++标准库中没有任意精度类型的实现,因此您必须自己实现,或者可以使用其他人实现的库。 Fundamental integral types such as long long have limited range that ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)