Answered by:
Date & Time

Question
-
User-797751191 posted
Hi
I have below code . In Session variable i want to display only Date
Session["PostingDate"] = Convert.ToDateTime(PostingDate.ToString("yyyy-MM-dd"));
Thanks
Monday, June 24, 2019 7:42 AM
Answers
-
User665608656 posted
Hi jsshivalik,
According to your code, I am not sure what is the txtPostingDate.Text format?
If you just want to convert the content of txtPostingDate.Text to a date format and save it in session,you could write like this:
Session["PostingDate"] = Convert.ToDateTime(txtPostingDate.SelectedDate.ToString("yyyy-MM-dd")).ToShortDateString();
If not, please tell us what is the txtPostingDate.Text specific value?
Best Regards,
YongQing.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, June 24, 2019 10:09 AM
All replies
-
User665608656 posted
Hi jsshivalik,
According to your description,I suggest you to use ToShortDateString() method belong to DateTime type.
You could refer to this link: https://docs.microsoft.com/en-us/dotnet/api/system.datetime.toshortdatestring?view=netframework-4.8
You can adapt your code like this:
Session["PostingDate"] = PostingDate.ToShortDateString();
Best Regards,
YongQing.
Monday, June 24, 2019 8:40 AM -
User-797751191 posted
Hi Yu
I have exactly this code
var PostingDate = DateTime.ParseExact(txtPostingDate.Text, "dd-MM-yyyy", CultureInfo.InvariantCulture);
Session["PostingDate"] = Convert.ToDateTime(PostingDate.ToString("yyyy-MM-dd"));
Thanks
Monday, June 24, 2019 9:43 AM -
User665608656 posted
Hi jsshivalik,
According to your code, I am not sure what is the txtPostingDate.Text format?
If you just want to convert the content of txtPostingDate.Text to a date format and save it in session,you could write like this:
Session["PostingDate"] = Convert.ToDateTime(txtPostingDate.SelectedDate.ToString("yyyy-MM-dd")).ToShortDateString();
If not, please tell us what is the txtPostingDate.Text specific value?
Best Regards,
YongQing.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, June 24, 2019 10:09 AM -
User-797751191 posted
Hi
What is SelectedDate
Thanks
Monday, June 24, 2019 10:47 AM -
User665608656 posted
Hi jsshivalik,
Sorry,I forgot to tell you about it.
Because I don't know what your txtPostingDate.Text stands for, I created a calendar control and set the ID to txtPostingDate.
So here txtPostingDate.SelectedDate.ToString ("yyyy-MM-dd") is a dateTime string the user selected on the calendar.
Ok,I simulated a date-type string and operated in your way:
var PostingDate = DateTime.ParseExact("2019-06-25 10:58:52", "yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture); Session["PostingDate"] = Convert.ToDateTime(PostingDate.ToString("yyyy-MM-dd")).ToShortDateString();
You could refer to this link: https://stackoverflow.com/questions/919244/converting-a-string-to-datetime
Best Regards,
YongQing.
Tuesday, June 25, 2019 2:59 AM