• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 88
  • Last Modified:

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
0
mainrotor
Asked:
mainrotor
  • 6
  • 2
  • 2
  • +2
2 Solutions
 
mgfranzCommented:
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
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
mgfranzCommented:
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
 
mgfranzCommented:
Yup!  And like I said, format the DTS at the DB when populating instead of later is best.
1
 
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
 
mgfranzCommented:
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
 
mgfranzCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

  • 6
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now