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);
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 need to send a date to a backend service that requires a date in the following format.
I have access to moment also.
I am using an input type of datetime on the front end which sends over a date like this: "2017-05-17T10:00"
I have tried new Date("2017-05-17T10:00"); but this returns Wed May 17 2017 11:00:00 GMT+0100 (BST). I have also tried using some moment methods, but cannot get the correct format.
Does anyone know how I can convert the datetime string - "2017-05-17T11:43" to the following '2017-05-17T10:43:03+0100'?
Try moment.format(). Here is the list for reference https://momentjs.com/docs/#/displaying/format/
var dt = new Date("2017-05-17T10:00");
console.log(dt);
//'2017-05-17T10:43:03+0100'
var z = moment(dt).format("YYYY-MM-DDTHH:mm:ssZZ");
console.log(z);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
Your date is in ISO 8601 format. If you have access to Moment.js (as you said) you can use format() method as below:
var date = moment("2017-05-17T10:00").format("YYYY-MM-DDTHH:mmZZ");
console.log(date);
// prints "2017-05-17T10:00-0300"
Try it.
I am using bootstrap date range picker and I am getting the input date from
var startDate = $('#reportrange').data('daterangepicker').startDate._d;
and the output will look like
"Sun Mar 10 2012 05:50:34 GMT +0600"
But I want the output as timestamp without timezone which is
2012-03-10 05:50:34
So I tried
var s = startDate.format('YYYY-MM-DD HH:mm:ss');
But it didn't give the result.I do not want this as a string and as a timestamp only.Any help is appreciated.
You cannot use format() on a string and expect it to format into date. Either you will have to write your own code to parse the string and convert into date and then into particular date format. Or you can use a library like moment.js.
This question has been answered in detail here How to format a JavaScript date
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");
The format of my date string looks like this: yyyy-MM-ddTHH:mm:ss-0Z00
Example 1: 2010-03-05T07:03:51-0800
Example 2: 2010-07-01T20:23:00-0700
I need to create a date object using these date strings. new Date() does not work on this string.
Please help me convert these date strings into a date objects with the local timezone.
Thank you!
Edit: I am using this in Pentaho Data Integration 4.3.0.
Take my timezone as an example (AEST):
function parseDate(str_date) {
return new Date(Date.parse(str_date));
}
var str_date = "2015-05-01T22:00:00+10:00"; //AEST time
var locale_date = parseDate(str_date);
locale_date: Fri May 01 2015 22:00:00 GMT+1000 (AEST)
var str_date = "2015-05-01T22:00:00+00:00" //UTC time
var locale_date = parseDate(str_date);
locale_date: Sat May 02 2015 08:00:00 GMT+1000 (AEST)
You can use a library such as Moment.js to do this.
See the String + Format parsing.
http://momentjs.com/docs/#/parsing/string-format/
The following should parse your date you provided, but you may need to modify it for your needs.
var oldDate = "2010-03-05T07:03:51-0800";
var dateObj = moment(oldDate, "YYY-MM-DDTHH:mm:ssZ").toDate();
Alternatively, see Moment's String parser, which looks like it is in the format you provided, with the exception of a space between the seconds of the time and the time zone.
http://momentjs.com/docs/#/parsing/string/
Alternative
A second way of doing this is Date.js, another library that seems to parse the format just fine. http://www.datejs.com
Date String:
var strDate = "2010-07-01T20:23:00-0700";
To local time representation in native JS Date object:
var ltzDate = (new Date(strDate)).toLocaleString();