I want to convert DataTime.Now output into a interger,
I want to remove all the forward slash "/" Colons ":" and spaces " " even PM/AM.
example:
From = 1/24/2022 10:51:54 PM
to = 1242022105154
CodePudding user response:
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.
CodePudding user response:
If you have a date-string you first need to parse it, then you can use 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.
Note that m
are minutes and M
is month, uppercase H
means 24h hours.
See: Custom date and time format strings
CodePudding user response:
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.
CodePudding user response:
You can use as below:
From.ToString().Replace("/","").Replace(":","").Replace(" ","")
CodePudding user response:
You can use :
.Replace("the character u want to remove" , "new char");