I need help formatting and displaying a date value in my HTML page

Hi Experts,
I have an MVC.Net application where I display various day values.
The values are retrieved from a SQL Server Database.  
The way that the values are saved in the database are as follows:  2017-11-13 16:45:57:263

I want to display the date in my input field with the following format:  11/13/2017.  How can I do this?

Thank you very much in advance,
mrotor
mainrotorAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mark FranzProject ManagerCommented:
Try this;

Dim varDate As String = DateTime.Now.ToString("MM, dd, yyyy")

Open in new window

0
Pawan KumarDatabase ExpertCommented:
>>I have an MVC.Net application where I display various day values. The values are retrieved from a SQL Server Database.
We can do  this change in the DB itself , in the SQL Server itself.

Eg. below

/*------------------------
SELECT FORMAT(CAST('2017-11-13 16:45:57:263' AS DATETIME ) , 'MM/dd/yyyy' )
------------------------*/

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
11/13/2017

(1 row(s) affected)

Open in new window

0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Mark FranzProject ManagerCommented:
Server-side on page load might use less threads than using SQL based query since the OP stated "The values are retrieved from a SQL Server Database".  I guess it would depend on how many fields are being pulled and how much data is being filled and how the parent page is being built.  Besides, if the SQL needs the original DTS for other values doing it DB side is moot.  Another thought is to write the DTS directly into the DB field in the format desired at run-time would be another option.
0
Pawan KumarDatabase ExpertCommented:
This formatting part is very very quick. We should not be worry about this. The idea is to format all the things at one place( one query rather at multiple places on multiple pages of the application.
0
Mark FranzProject ManagerCommented:
Yup!  And like I said, format the DTS at the DB when populating instead of later is best.
1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Snarf0001Commented:
I'm going to have to go with mgfranz on this one.
Converting on the database side for end display purposes is very rarely if ever a good idea.

1) You're most likely already changing some formatting when rendering out your view anyway
2) Depending on servers, in almost all cases .NET will do the conversion better / faster
3) You would then be losing precision if you needed the real value for anything else, which happens frequently (lost all time components)
4) Would be taking up MORE space on the transfer from the database as a character

Basic rule of thumb, let the database do what it's good at.  Finding, filtering, sorting data.
When it comes time to actually show the data, let your front end presentation layer handle that.
0
Nakul VachhrajaniTechnical Architect, Capgemini IndiaCommented:
Agree. The database engine is primarily responsible for the storage, retrieval and manipulation of data.

Representational requirements are of the fron-end application and that should take care of formatting in the required format.

This is not only true for dates, but for any data-type (numbers, currency, etc).
0
mainrotorAuthor Commented:
mgFranz,

How can i apply your code in my view:
Dim varDate As String = DateTime.Now.ToString("MM, dd, yyyy")


Here is a snippet of my code in my view:
$("#StartDate").val(selectedItem.StartDate);

Open in new window


How can I apply your formatting to my code above?

thanks in advance,
mrotor
0
Mark FranzProject ManagerCommented:
I use it at run time;
    
Public Sub Page_Load()
        Dim varDate As String = DateTime.Now.ToString("d MMM, yyyy")
        dateBox.Text = varDate
End Sub

Open in new window


Is that jQuery code you are using?  If so, I'm not really an expert on jQuery, but perhaps something like this?

$("#StartDate").val(.datepicker.formatDate( dd, mm, yy, (selectedItem.StartDate)));

Open in new window


Untested and probably wrong, plus I don't think you can parse a combobox item, (if that is what it is), into a jQuery field like that, perhaps if you post your .net code where you are filling the combobox I can see if we can fill the vals at runtime.
0
Snarf0001Commented:
Please show a more complete code sample from your view or model.
You're showing us the javascript where it's being selected, but not showing the population "selectedItem".
It's that object we need to see and which should be formatted.
0
Mark FranzProject ManagerCommented:
Since the OP has gone off the grid, lets close this.

I'm guessing he does not has the capability of accessing the SQL to change the parameters for modifying the data at storage, so unless he can pull the data then modify it prior to dropping into a field...  But again, without more code we're stuck.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET MVC

From novice to tech pro — start learning today.