从给定日期获得月份数 [英] get month number from given date

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

问题描述

如何从给定日期获得月份数,假设给定日期是2014年1月21日

输出:1月。怎么做??? div class =h2_lin>解决方案

试试这个



  string  name = DateTime .ParseExact(  2014年1月21日   MM / dd / yyyy null )。ToString(  MMMM);  //   1月 


如果你输入的是一个字符串,你需要解析 [ ^ ]它为了获得 DateTime 对象,然后根据您的需要使用DateTime.Month [ ^ ]属性或使用< a href =http://msdn.microsoft.com/en-us/library/zdtaw1bw(v=vs.110).aspx> DateTime.ToString [ ^ ]方法,传递 M(或MM)作为参数。


你好,找出月份:看下面的代码:





  String  [] s = DateTime.Now.ToString()。Split('   - ',< span class =code-string>' '); 


for int i = 0 ; i < s.Length; i ++)
{

switch (i)
{
case 0
Console.WriteLine( 日期为:{0}, S [1]);
break ;
case 1
if (s [i] == 01 /// /此检查仅适用于1月,您可以为所有其他月份执行
Console.WriteLine( 月份1月);
break ;
}

}







我给了你一个逻辑。然而,这是一个非常糟糕和冗余的编码。它只是一种方式,你可以用它来开发自己的逻辑。



只需确认一件事:在控制台上打印日期时间。如果你这样做,你可以决定在开关盒中写入哪个条件。





你需要知道如何格式化和操作在特定的字符串上:我使用了字符串的split方法。有多种方式,谷歌他们。



为什么甚至做上述所有事情?简单地说:





 Console.WriteLine(DateTime.Now.ToString(  MM)); 







是什么原因可以进行上述所有练习?只是发展你的逻辑?如果没有帮助怎么办?这些都只是包装纸。你需要自己写一些东西。只是从这种情况中成长。



谢谢,

快乐学习。


how to get month number from given date,Suppose given date is 01/21/2014
output:january .how to do that??

解决方案

Try this

string name = DateTime.ParseExact("01/21/2014", "MM/dd/yyyy", null).ToString("MMMM"); //January 


If you input is a string the you have to parse[^] it in order to get a DateTime object, then, depending on your needs, you use the DateTime.Month[^] property or use the DateTime.ToString[^] method, passing "M" (or "MM") as argument.


Hello, to find out the month: look at the following code:


String[] s = DateTime.Now.ToString().Split('-',' ');


       for (int i = 0; i < s.Length; i++)
       {

           switch (i)
           {
               case 0:
                   Console.WriteLine("Date is :{0}",s[i]);
                   break;
               case 1: 
                   if(s[i] == "01")////this check is just for january, you can do it for all other months
                   Console.WriteLine("Month January");
                   break;
           }

      }




I have given you a logic. However this is a very bad and redundant coding. Its just a way so that you can use this to develop your own logic.

Just confirm one thing: print the date time on the console. If you do so you can then decide which condition to write in the switch case.


You need to know how to format and operate on a particular string: i have used string's split method. There are multiple ways just, google them.

Why even do all the above things? simply say :


Console.WriteLine(DateTime.Now.ToString("MM"));




Whats the reason to do all the above exercise? To just develop your logic? What if there was no help. These are all just wrappers. You need to write something of your own. Just grow from the situation.

Thanks,
Happy learning.


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

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