Click here to Skip to main content
15,896,549 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Scenario:

Have two input texts, i need the get the user entered values out of the input texts. Combine them and get the java script date out of it


Code
JavaScript
var UserStartTime = Control1.value;//string hh,mm,ss
var UserStartDate = Control2.value//string yyyy,mm,dd

var DateString = UserStartDate +","+ UserStartTime;//string ex:2013,11,14,03,00,00
var newTime = (new Date(DateString));//this fails and returns invalid date



Issue:

The code returns invalid date because of double quotes. Value of DateString = "2013,11,14,03,00,00". How do i remove the double quotes from this string and convert that value to JS date?

What i have tried so far and didn't work:
JavaScript
var NonQuotesVaraible = new String(DateString);
var temp = Number(NonQuotesVaraible );

DateString.replace(/"/g, ""); 

DateString.replace(/["]/g, "");

DateString.replace(/\"/g, "");

JSON.parse(DateString);

DateString.replaceAll("^\"|\"$", "");

DateString.replace("\"","");

DateString.valueOf();

In all the cases double quotes are still present.
Posted
Updated 12-Oct-20 4:38am
v2
Comments
Sergey Alexandrovich Kryukov 14-Nov-13 17:33pm    
This is not a problem. The problem is: how come unwanted characters got to you string in first place? Isn't it better to prevent it? Normal date formats don't have ""...
—SA
amarasat 15-Nov-13 8:55am    
I dont understand, what do you mean by normal date formats? A user has entered the text (2013,11,14) in an html input text box. When i assign a variable (xyz) and get the value of the input text box from the DOM like xyz = element.value. Then xyz is "2013,11,14" with quotes, its not 2013,11,14

When you are using
DateString.replace("\"","");

perhaps you should be using...
DateString = DateString.replace("\"","");

This will make a big difference because string.Replace() returns the modified string, it doesn't modify the string you call it on.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 14-Nov-13 17:49pm    
5ed. We answered second-to-second... :-)
—SA
amarasat 15-Nov-13 8:57am    
CraigDJ, ofcourse i have used DateString = DateString.replace("\"","");. I just didn't copy and paste the whole line of code here.

DateString = DateString.replace("\"",""); This doesnt remove the double quotes
Sergey Alexandrovich Kryukov 15-Nov-13 10:15am    
CrageDJ,

You really, really need to take a look at comments to Solution 2, to get an idea who we are dealing with...

(Sigh...)

—SA
This will work to remove all occurrences of ":
JavaScript
DateString = DateString.replace(/"/g, '');


I looks like you already had the similar variant. What's bad is that you are trying to use trial-and-error method while you could rather read some help document and just think few seconds: http://www.w3schools.com/jsref/jsref_replace.asp[^].

Now, please see my comment to the question. I don't see why those unwanted characters should appear in your string in first place.

—SA
 
Share this answer
 
v2
Comments
amarasat 15-Nov-13 9:03am    
No this didn't work either.

EndDateAndTime = "2013,11,14,03,00,00";
EndDateAndTime = EndDateAndTime.replace(/"/g, '');
var CurrentTime = (new Date(EndDateAndTime));
CurrentTime value is Date {Invalid Date}
Sergey Alexandrovich Kryukov 15-Nov-13 10:13am    
You probably don't even understand: in the string in your first expression there are no quotation characters. Listen carefully: here
EndDateAndTime = "2013,11,14,03,00,00";
The object EndDateAndTime has zero count of ". Zero! There is no one.

Everything works.

Are you finally getting the idea? You are messing up elements of JavaScript syntax with content of memory object is operates.
You whole question makes no sense at all.

—SA
amarasat 15-Nov-13 10:31am    
you said "Everything works". Ok may be i misunderstood strings. To explain this better i have constructed this jsfiddle.

http://jsfiddle.net/GV4Ww/8/

Can you make that work? When user types a time or date in the form i have shown in the input boxes in the same format and click apply, can you alert the date?

See how Show2 sjhows the date but Show shows and invalid date
Sergey Alexandrovich Kryukov 15-Nov-13 10:48am    
(Sigh...) The link did not work...
—SA
amarasat 15-Nov-13 10:57am    
Sorry SA, i think i updated the link after a while. Its

http://jsfiddle.net/GV4Ww/9/. Are you sure this is the link that did not work?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900