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
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 date format like : 2018-10-04T20:35:28. in javascript.
I don't know what format is this, but I already try follow
Now I have this:
var now = new Date();
var isoDate = new Date(now).toISOString();
My output is:
2018-10-05T04:55:58.896Z
But I have a wrong day because actual date is:
Thu 4 Oct 2018 22:56:53 CST
Why i have +1 day in all dates.
like #Nisarg Shah said "The ISO string is in UTC, the one in console is in your local time zone" . You can change it using this
new Date().toLocaleString("en-US", {timeZone: "America/New_York"})
Check this out for more information.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleString
var isoDate = new Date(now).toISOString();
// Output
2018-10-05T04:55:58.896Z
This isoDate is in UTC. You can see that there is a 'Z' in the end of the string. This means that the date is in UTC.
You can use Moment Timezone (moment.js) to convert any given date to another timezone.
moment.tz('2018-10-05T04:55:58.896Z', 'America/Toronto').format();
Just change the timezone name to the one you want to convert.
For Further Details
https://momentjs.com/timezone/docs/#/using-timezones/
I Have date as a string 10/06/1991 when I need to convert to a date object the day and month value is swiping like this Sun Oct 06 1991 00:00:00 GMT+0200 (GMT+03:00), but if I reverse my string to 1991/06/10 it's correct after converting.
is there any idea how to solve it?
You cannot parse string to date using Date object unless it is in ISO form. Here later date - 1991/06/10 is in in correct order so js can make correct date object.
For your case, you can use any other library like moment for parse your given date 10/06/1991 by giving its format to moment, example is given below -
console.log(moment("10/06/1991", "DD/MM/YYYY").toDate());
<script src="https://momentjs.com/downloads/moment-with-locales.min.js"></script>
I am receiving date from back end in 2017-03-02T08:12:22.997000+00:00 this format.
To display this date in specified format I am doing
new Date('2017-03-02T08:12:22.997000+00:00').toLocaleString() that gives 3/2/2017, 1:42:22 PM
There's one functionality which is sorting this formatted output.
Agenda is to sort the output based on 'date' type. But since I am using toLocaleString() method for formatting, sorting is done based on 'string' type.
Is there any solution where I can achieve 3/2/2017, 1:42:22 PM format and type will be a Date object?
Or a date format where I can see date along with time excluding GMT part? (like toUTCString())
Or any method from moment will work?
you can use moment.js which will give you moment object.
first convert date to IsoDate as moment.js latest version has deprecated moment constructor with illegal date string ( more info here )
var isoDate = new Date('2017-03-02T08:12:22.997000+00:00').toISOString(),
formatedDate = moment(isoDate).format('DD/MM/YYYY, HH:MM:SS A');
// formatedDate : "02/03/2017, 13:03:99 PM"
you may use Timezone for better handling of time -
moment(isoDate).tz('timezoneValue').format('DD/MM/YYYY, HH:MM:SS A');
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();