Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Trouble with Date formatting in Sql Server 2008, ASP.net, and then Javascript

Posted on 2014-12-05
7
Medium Priority
?
244 Views
Last Modified: 2014-12-05
I'm writing an Asp.net in C# application that retrieves data from Sql Server 2008 and converts it to JSON so it can be placed on a Google Map.  Trying to figure out the proper way to handle date formatting is driving me crazy.

In Sql Server, the date column is an nvarchar (and there is nothing I can do about that).   Sometimes this column is like 18-OCT-10 17:36, 4/8/2009 11:34:00 AM, 3/3/11, or is just null or blank.  For testing I'm retrieving rows that are 6/15/14.  If I don't do any CASTS or CONVERTS with t-sql and bring it into my asp.net application like this...
dsGetFieldPoints.SelectParameters["trackID"].DefaultValue = trkID;
var fieldPts = dsGetFieldPoints.Select(DataSourceSelectArguments.Empty) as DataView;
var fieldPtMarkers = new List<FieldPoint>();

            foreach (DataRowView pt in fieldPts)
            {
                FieldPoint fp = new FieldPoint();
                fp.locDate = (string)pt["locDate"];
                fieldPtMarkers.Add(fp);
            }
            JavaScriptSerializer js = new JavaScriptSerializer();
            var fieldPtsJson = js.Serialize(fieldPtMarkers);
            var fieldPtsJsonFinal = "{\"fieldPoints\": " + fieldPtsJson + "}";
            ClientScript.RegisterStartupScript(this.GetType(), "Google Maps Initialization and Add Pts",
                      string.Format("initializeMap({0}, {1});", photosJson, fieldPtsJsonFinal), true);

Open in new window


In the JSON output the date looks like this - "6/15/14", but when I use javascript....
var fDate = new Date(fMarker.locDate);
fDate.toDateString()

Open in new window

I get this Mon  6/14/1914.   So I'm using a With clause in my t-sql statement and I try this....
With fieldPts AS 
		(SELECT 		
		CAST(ISNULL(fp1.Loc_Date, '') AS DATE) AS locDate....)
SELECT 
CAST(fp.locDate AS NVARCHAR) AS locDate....

Open in new window

In the JSON output the date looks like this "2014-06-15", but when I use the javascript it looks like this Sat  6/14/2014.   So I try not converting it back to nvarchar....
With fieldPts AS 
		(SELECT 		
		CAST(ISNULL(fp1.Loc_Date, '') AS DATE) AS locDate....)
SELECT 
fp.locDate....

Open in new window


And then updating my asp.net code to .....
fp.locDate = (DateTime)pt["locDate"];

Open in new window

the JSON output looks like this "\/Date(1402808400000)\/" and the javascript produces undefined  Invalid Date.
In my t-sql I've tried using CONVERT...
convert(date, CAST(ISNULL(fp1.Loc_Date, '') AS DATE), 112) AS locDate

Open in new window

but it doesn't seem to work since I always get the same format of 2014-06-15.   What can I do now?  Thanks
0
Comment
Question by:Nate_LR
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 29

Expert Comment

by:sammySeltzer
ID: 40483450
I had similar situation and this is what worked for me:

convert(char(10),fp1.Loc_Date,101) AS locDate

Open in new window


See if this is any help to you.
0
 

Author Comment

by:Nate_LR
ID: 40483533
Sammy I tried this... T-sql
With fieldPts AS 
		(SELECT 		
		convert(char(10),fp1.Loc_Date,101) AS locDate,....)
SELECT 
fp.locDate....

Open in new window

Asp.net
fp.locDate = (string)pt["locDate"];

Open in new window

the date in the JSON output is "6/15/14   " but when I use the javascript...
var fDate = new Date(fMarker.locDate);
fDate.toDateString()

Open in new window

I get this Mon Jun 15 1914.  

The other thing I can't figure out is if I change the recommendation you gave to...
convert(char(10),fp1.Loc_Date,102) AS locDate,

Open in new window

it should give me 2014.06.15 but when I run the stored procedure I still get 6/15/14.
0
 
LVL 29

Expert Comment

by:sammySeltzer
ID: 40483565
When you use my recommendation, you should get 6/15/2014.

First, what is the data type of your date?

Second, what format are you looking for?

Ok, I read again and you said nvarchar but what format do you want?
0
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 

Author Comment

by:Nate_LR
ID: 40483588
The data type is nvarchar.  I'd like to convert it to date somewhere, but that might prove difficult with all the varieties of date formats in that column (18-OCT-10 17:36, 4/8/2009 11:34:00 AM, 3/3/11, or is just null or blank).
0
 
LVL 29

Accepted Solution

by:
sammySeltzer earned 2000 total points
ID: 40483609
One last question, you didn't answer this one in my last question.

What format do you want it?

In the meantime, try this:

select convert(varchar,cast(locdate as date),101)

I know you want to convert it to date  but what format?

Also, I just realized that it is already in nvarchar data type, so we can't possibly be still using convert(varchar...

It should be something like convert(date, datefield, number).

Get back with me first and we will try something.

In the meantime, you can try this:

select convert(varchar,cast(fp1.Loc_Date,101 as date),101)

Open in new window


This should give you format of mm/dd/yyyy
0
 

Author Comment

by:Nate_LR
ID: 40483684
You had an extra 101 in your solution, but I tried this...
convert(varchar,cast(fp1.Loc_Date as date),101) AS locDate

Open in new window

And it worked.  Thanks sammy!
0
 
LVL 29

Expert Comment

by:sammySeltzer
ID: 40483715
I am glad you caught. That was a typo and even happier that it worked for you.
0

Featured Post

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question