简体   繁体   English

如何从 DateTime.Now 格式中删除所有正斜杠“/”、冒号“:”和空格

[英]How can i remove all the forward slash "/", Colons ":" and spaces from DateTime.Now format

I want to convert DataTime.Now output into a interger,我想将 DataTime.Now output 转换为整数,

I want to remove all the forward slash "/" Colons ":" and spaces " " even PM/AM.我想删除所有正斜杠“/”冒号“:”和空格“”,甚至 PM/AM。

example:例子:

From = 1/24/2022 10:51:54 PM从 = 2022 年 1 月 24 日晚上 10:51:54

to = 1242022105154至 = 1242022105154

Use this:用这个:

long n = long.Parse(date.ToString("yyyyMMddHHmmss"));

if you want in string format:如果你想要字符串格式:

var n = date.ToString("yyyyMMddHHmmss");

And you can change the location of the year and month in the above field.您可以在上述字段中更改年份和月份的位置。 for example:(this is correct for your question)例如:(这对您的问题是正确的)

var n = date.ToString("MddyyyyHHmmss");

where date is your variable contain date value.其中日期是您的变量包含日期值。

If you have a date-string you first need to parse it, then you can use DateTime.ToString :如果你有一个日期字符串,你首先需要解析它,那么你可以使用DateTime.ToString

string s = "1/24/2022 10:51:54 PM";
string result = DateTime.Parse(s, CultureInfo.InvariantCulture).ToString("MdyyyyHHmmss");

So the correct format string for you is MdyyyyHHmmss.所以对你来说正确的格式字符串是 MdyyyyHHmmss。

Note that m are minutes and M is month, uppercase H means 24h hours.请注意, m是分钟, M是月份,大写H表示 24 小时制。

See: Custom date and time format strings请参阅: 自定义日期和时间格式字符串

You can use as below:您可以使用如下:

From.ToString().Replace("/","").Replace(":","").Replace(" ","")

You can use:您可以使用:

.Replace("the character u want to remove" , "new char");
string outPut= DateTime.Now.ToString("MdyyyyHHmmss", CultureInfo.InvariantCulture);

The Path.路径。 GetFileNameWithoutExtension method gives you the filename you pass as an argument without the extension, as should be obvious from the name. GetFileNameWithoutExtension方法为您提供了作为不带扩展名的参数传递的文件名,从名称中应该很明显。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM