删除前导零表单C# [英] Remove leading zero form month C#

查看:120
本文介绍了删除前导零表单C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  DateTime date1 = new DateTime(2008,8,18); 
Console.WriteLine(date1.ToString((M)MMM,MMMM,
CultureInfo.CreateSpecificCulture(en-US)));
//显示(8)八月,八月

完全不在这里工作/ p>

这是我的代码:

  string date ='2013-04 -01'
DateTime billrunDate = Convert.ToDateTime(date);
string test = billrunDate.ToString(M);

现在测试 4月01日



我只需要它是一个字符串或int idc
4 谢谢!



如果我做,编辑:

  billrunDate.ToString((M)); 

我得到(4),但我不需要()



编辑2:
这样工作

  string test = billrunDate.ToString(M); 
string testTwo = test.Trim();

非常非常丑陋

解决方案

M 解释为月日模式的标准日期和时间格式



将单个字符模式解释为自定义日期和时间模式,只需在

  string test = billrunDate.ToString(%M); 


I'm having trouble to remove the leading zero from a date I found this on the miscrosoft website.

DateTime date1 = new DateTime(2008, 8, 18);
Console.WriteLine(date1.ToString("(M) MMM, MMMM", 
                  CultureInfo.CreateSpecificCulture("en-US")));
// Displays (8) Aug, August

Totally doesn't work here.

This is my code:

string date = '2013-04-01'
DateTime billrunDate = Convert.ToDateTime(date);
string test = billrunDate.ToString("M");

Test is now 01 April

I just need it to be 4 in a string or int idc Thanks!

Edit if I do:

billrunDate.ToString("(M)");

I get (4), but I dont need ()

EDIT 2: Well this works

string test = billrunDate.ToString(" M ");
string testTwo = test.Trim();

Very very ugly

解决方案

It's interpreting M as a standard date and time format for "month day pattern".

To interpret a single character pattern as a custom date and time pattern, just prefix it with %:

string test = billrunDate.ToString("%M");

这篇关于删除前导零表单C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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