Answered by:
Change string to date format -ddMMMYYYY

Question
-
User-73514677 posted
Hi,
I have a string column which contains the data in the format
25/11/2017 00:00:00
I want to convert it in the format of
25Nov2017
and then reduce 2 days in it , so that the text becomes
23Nov2017
How to achieve this in C#?
Thanks
Tuesday, February 19, 2019 3:36 PM
Answers
-
User-1174608757 posted
Hi venkatzeus,
According to your description, I have made a sample here.You could use c# split() method get the part of 25/11/2017, then again use split get the month value ,lastly you could use switch method to change the value of moth to the Jan or Feb etc.Here is the demo , I hope it could help you.
You could just the put the method in the pageload event if you want the value of date firstly show just as you want .
Aspx:
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:TextBox ID="TextBox1" runat="server" TextMode="DateTime" ></asp:TextBox> <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" /> </div> </form> </body> </html>
Aspx.cs:
public partial class timeformat : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { string s = TextBox1.Text; string [] s1 = s.Split(' '); string s2 = s1[0];// get the date string[] s3 = s2.Split('/'); string month = s3[1];// get the month switch (month) { case "1": month = "Jan"; break; case "2": month = "Feb"; break; case "3": month = "Mar"; break; case "4": month = "Apr"; break; case "5": month = "May"; break; case "6": month = "Jun"; break; case "7": month = "Jul"; break; case "8": month = "Aug"; break; case "9": month = "Sep"; break; case "10": month = "Oct"; break; case "11": month = "Nov"; break; case "12": month = "Dec"; break; } string date = (Convert.ToInt32 (s3[0])-2).ToString() + month + s3[2]; // reduce the day 2
Response.Write(date); } }It shows as below:
Best Regards
Wei Zhang
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, February 20, 2019 2:26 AM
All replies
-
User475983607 posted
Hi,
I have a string column which contains the data in the format
25/11/2017 00:00:00
I want to convert it in the format of
25Nov2017
and then reduce 2 days in it , so that the text becomes
23Nov2017
How to achieve this in C#?
Thanks
The best approach is fixing the design bug by assigning the column to a DateTime type rather than a string.
The alternative is converting the date string into a DateTime type either in TSQL or C#. Once the string is converted to a DateTime type it is very easy to add/subtracts days and convert to another string format using standard .NET string formatters.
https://docs.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings
Tuesday, February 19, 2019 4:06 PM -
User-1174608757 posted
Hi venkatzeus,
According to your description, I have made a sample here.You could use c# split() method get the part of 25/11/2017, then again use split get the month value ,lastly you could use switch method to change the value of moth to the Jan or Feb etc.Here is the demo , I hope it could help you.
You could just the put the method in the pageload event if you want the value of date firstly show just as you want .
Aspx:
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:TextBox ID="TextBox1" runat="server" TextMode="DateTime" ></asp:TextBox> <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" /> </div> </form> </body> </html>
Aspx.cs:
public partial class timeformat : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { string s = TextBox1.Text; string [] s1 = s.Split(' '); string s2 = s1[0];// get the date string[] s3 = s2.Split('/'); string month = s3[1];// get the month switch (month) { case "1": month = "Jan"; break; case "2": month = "Feb"; break; case "3": month = "Mar"; break; case "4": month = "Apr"; break; case "5": month = "May"; break; case "6": month = "Jun"; break; case "7": month = "Jul"; break; case "8": month = "Aug"; break; case "9": month = "Sep"; break; case "10": month = "Oct"; break; case "11": month = "Nov"; break; case "12": month = "Dec"; break; } string date = (Convert.ToInt32 (s3[0])-2).ToString() + month + s3[2]; // reduce the day 2
Response.Write(date); } }It shows as below:
Best Regards
Wei Zhang
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, February 20, 2019 2:26 AM -
User-73514677 posted
Hi,
Thanks. It worked
Thursday, February 21, 2019 2:40 PM