不支持给定格式日期时间错误 [英] The given format is not supported Datetime error

查看:200
本文介绍了不支持给定格式日期时间错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试导出文件。



以下是路径的代码。



I am trying to export a file.

Below is the code for the path.

 string date = Convert.ToString(DateTime.Now);

string namefile = "Stock_Flow_Report_2015" + date;





当我删除日期时,它运行正常。但是当我把日期显示为不支持给定格式的例外。



请帮忙解决这个问题。



When i remove date, it is working fine. But when I put the date it shows the exception that The given format is not supported.

Please help regarding this.

推荐答案

很多字符都是非法的名字:? * \和/ fit示例。



系统上的默认日期时间格式可能是dd / MM / yyyy或MM / dd / yyyy



所以试试这个:

Quite a lot of characters are illegal in for names: ? * \ and/ fit example.

And the default date time format on your system is likely to be dd/MM/yyyy or MM/dd/yyyy

So try this:
string namefile = "Stock_Flow_Report_2015" + DateTime.Now.ToString("dd-MM-yyyy");

或更好:

Or better:

string namefile = DateTime.Now.ToString("yyyy-MM-dd") +  "Stock_Flow_Report_2015;


您可以在一行中实现所需,而无需创建辅助字符串变量:

What you want could be achieved in one single line, without having to create a secondary string variable:
// C# 5-
string namefile = string.Format("Stock_Flow_Report_2015{0}", DateTime.Now);

// OR
// C# 6
string namefile = string.Format(


Stock_Flow_Report_2015 {DateTime.Now});





string.format()将把DateTime值转换为字符串。



希望这会有所帮助。



string.format() will take care of converting the DateTime value to string.

Hope this helps.


这篇关于不支持给定格式日期时间错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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