如何使用C#从当前日期获取当前季度 [英] How to get Current Quarter from Current Date using C#

查看:1618
本文介绍了如何使用C#从当前日期获取当前季度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从当前日期获取当前季度和它第一次存储为int,再经过我得到的当前财季喜欢说,这是Q1然后我想存储为Q1字符串。我得到读取这样的错误:未分配的局部变量DT。 。请帮忙。谢谢

 的DateTime DT;
INT quarterNumber =(dt.Month - 1)/ 3 + 1;


解决方案

好吧你不指定当前日期的任何地方 - 你没有一个值分配给 DT 变量,这是编译器的抱怨。您可以使用:

  DateTime的DT = DateTime.Today;

请注意,这将使用该系统的本地时区 - 和日期取决于时间区。如果你想在UTC当前时刻的日期,例如,你想要的:

  DateTime的DT = DateTime.UtcNow.Date;

想得很仔细考虑你所说的今天是什么。

另外,你的计算稍微简单的替代版本将会是:

 季度整型=(月+ 2)/ 3;

I am trying to get the current quarter from current date and store it as int first, then after i get the current quarter like say it is Q1 then i want to store Q1 as string. I am getting an error that reads like this: unassigned local variable dt. . Please help. thanks

DateTime dt;
int quarterNumber = (dt.Month - 1) / 3 + 1;

解决方案

Well you're not specifying "the current date" anywhere - you haven't assigned a value to your dt variable, which is what the compiler's complaining about. You can use:

DateTime dt = DateTime.Today;

Note that that will use the system local time zone - and the date depends on the time zone. If you want the date of the current instant in UTC, for example, you'd want:

DateTime dt = DateTime.UtcNow.Date;

Think very carefully about what you mean by "today".

Also, a slightly simpler alternative version of your calculation would be:

int quarter = (month + 2) / 3;

这篇关于如何使用C#从当前日期获取当前季度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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