I have a text box that is populated from a datepicker in Javascript. The date is stored as 30-Jan-2013. I want to convert this to a date so I can use it in other calculations.
I have tried
var date1 = new Date(document.getElementById('textbox').value)
but this returns Nan
if I drop the new Date part e.g.
var date1 = (document.getElementById('textbox').value
I get the date 30-Jan-2013 I just don't seem to be able to convert this?
Looks like Date.parse is not going to work because the format the datepicker is returning.
You might want to look at the following thread for parsing solutions or change your format that the datepicker outputs to one of the supported formats.
How can I convert string to datetime with format specification in JavaScript?
http://blog.dygraphs.com/2012/03/javascript-and-dates-what-mess.html
Useful parse info:
http://msdn.microsoft.com/en-us/library/ie/ff743760(v=vs.94).aspx
I'd like to recommend you a simple, easy to use and fast library [moment.js][1]
moment("30-Jan-2013").format('MMMM Do YYYY, h:mm:ss a');
will return "January 30th 2013, 12:00:00 am"
Hope this will work for your problem
var date= document.getElementById('textbox').value.split("-");;
var date1 = new Date(date[2] + " " + date[1] + " " + date[0]);
date1 .toLocaleDateString("en-GB");
Related
Is there any way to convert any date string (not necessarily current date) (could be any format) to specific date format in Javascript. Like converting "MM-DD-YYYY" or "ddMMYYYY" to "DD-MMM-YYYY"?
I know that from current date as var date = new Date(), we can get time and hours but what to do in case of existing date string like "31/01/1999" to "31-JAN-1999".
Given the input date string can be of any format.
This is a common problem.
You should be able to do it with moment.js.
Ex.
moment("31/01/1999").formatWithJDF("dd - MM - yyyy");
Have a look here, https://momentjs.com/docs/ for more details.
Using DateFormatter.js
var date = new Date('2020-03-25 10:30:25');
var formatter = new DateFormatter();
displayFormat = 'D M d Y h:i:s';
var dateString = formatter.formatDate(date, displayFormat); // Wed Mar 25 2020 10:30:25
This can be done by first checking the input date format with the arrays or Map of regex provided, then need to convert that format to JS accepted format ISO format.
Once converted to ISO format this date now can be converted to any form with logic written for it in separate functions.
I am trying to convert today's date in particular format and this date format can be anything like - dd/mm/yyyy or yyyy.mm.dd and so on
var date = new Date();
var todaysDate = date.toString('mm/dd/yyyy');
However, this code is not working.
Is there any other way to get it done.
I am looking for a common way where today's date can be converted to any given format
You could use Moment JS -- my go to JS date library -- to do something like
Moment (new Date ()).format("MM/DD/YYYY")
Or, if you know the form of the datetime format of the input string
Moment(string, "DD/MM/yyyy").format("MM/DD/YYYY")
MomentJS has other advantages too like adding time or dates or diffing two datetimes.
Alternatively, you could use Date Class functions to get parts of a Datetime and custom print or format them.
See StackOverflow format JS Date
I think, it is useful for u.
var currentDt = new Date();
var mm = currentDt.getMonth() + 1;
var dd = currentDt.getDate();
var yyyy = currentDt.getFullYear();
var date = mm + '/' + dd + '/' + yyyy;
alert(date);
Okay so I have a table with a column date and date values like " 2013-05-03 " since the table is dynamically filled. I have converted the date using php and the date now looks
like " 03/05/2013"
php conversion:
<td> <?php echo date("d/m/Y", strtotime($row['date'])) ?> </td>
Since I have a edit functionality in my page and I also use
I want to convert the date back to its original format so that chrome could recognize it
var date = new Date(feed date in dd/mm/yyyy format);
$("#customer_date").val(date);
I tried the above method. But looks like the conversion doesn't happen.
what could be a different approach?
thanks!
Do a simple split and join
var date = "03/05/2013";
var newdate = date.split("/").reverse().join("-");
"2013-05-03"
As your date is really just a string the quickest way would be to split it like this.
date = "03/05/2013";
date = date.split("/").reverse().join("-");
$("#customer_date").val(date);
Example here http://jsfiddle.net/GNFGP/1
if you want more powerfull date formatting, you can use date format function written by Steven Levithan here
var date = dateFormat(new Date("Thu Oct 14 2010 00:00:00 GMT 0530 (India Standard Time)"), 'yyyy-mm-dd');
'2015-04-03 16:50:05' gives '03-04-2015 16:50' with below code
var formattedTime = '2015-04-03 16:50:05'.substr(0,10).split('-').reverse().join('-')+" "+'2015-04-03 16:50:05'.substr(11,5);
I am trying to parse a date string to javascript timestamp, the format of the string is 2013-01-01 12:00 AM but when I attempt to alert the output all I can get is NaN My attempt is:
var ts_begin = Date.parse($('#fromdate').val()+' '+$('#fromtime').val());
alert(ts_begin);
The parse() method is very picky on what date formats it accepts. You may want to look at another JavaScript library called Moment.js to parse your dates (it can also do a lot more with date/time values).
Date.parse can only use specific formats. It's not a fan of the lack of space before AM/PM. Try:
Date.parse($("#fromdate").val() + ' ' + $("#fromtime").val()
.replace(/[AP]M/, ' $&'));
var utc_timestamp = Date.UTC(now.getFullYear(),now.getMonth(), now.getDate(),
now.getHours(), now.getMinutes(), now.getSeconds(), now.getMilliseconds());
alert(utc_timestamp);
so, i need format JSON date from this format
"9/30/2010 12:00:00 AM", it is MM/DD/YYYY HH:MM:SS to format like this : DD/MM/YYYY, so i dont need info about hours, min and sec, and i need replace months and days from json, i tried some different ways but it always failed
i need do this using jQuery
also i didnt find any answer to formating this date type, all i found was formating date like this :/Date(1224043200000)/
so anyone have idea?
you can create a Date Object from a string like so:
var myDate = new Date(dateString);
then you can manipulate it anyway you want, one way to get your desired output is:
var output = myDate.getDate() + "\\" + (myDate.getMonth()+1) + "\\" + myDate.getFullYear();
you can find more at this elated.com article "working with dates"
Unfortunately your "from" dateformat is not the one which is implementation-independent in JavaScript. And all the other formats depends on the implementation, which means even if this format would be understood by most of the implementation I/you can't be sure for example how the DD and MM order would be parsed (I am almost sure it would be local regional settings dependent). So I would recommend to use a 3rd party (or your hand written) date parser to get a Date object out of your input string. One such parser you can find here:
http://www.mattkruse.com/javascript/date/
Because your question is not 100% clear for me, it's possible that you have your date in the format of /Date(number)/ which suggests that you are calling an ASP.Net service from your jQuery code. In this case during the JSON parse you can convert it to a Date object:
data = JSON.parse(data, function (key, value) {
// parsing MS serialized DateTime strings
if (key == '[NAME_OF_DATE_PROPERTY_IN_THE_JSON_STRING]') {
return new Date(parseInt(value.replace("/Date(", "").replace(")/", ""), 10));
// maybe new Date(parseInt(value.substr(6))) also works and it's simpler
}
return value;
});
The code below solved my problem:
var date = new Date(parseInt(d.data[i].dtOrderDate.replace("/Date(", "").replace(")/", ""), 10));
var day = date.getDate();
var monthIndex = date.getMonth();
var year = date.getFullYear();
Try something like this :
var date = new Date(parseInt(jsonDate.substr(6)));
where jsonDate is variable that stores your date