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.
Related
I have called an API and then get a date which is string format like '15/07/21-23:59:59'. But I want to convert this string into the actual date format like this:
**15/07/21** OR **2009-06-01T10:00:00.000**.
so how can I achieve this?
It's strange to see a response returning a formatted date expression. By the way, your task would be easily done with momentjs. Here is my snippet:
// since your date format is not a standard one, you would have to pass an
// instruction of your date format as a second parameter to the moment constructor
const momentDate = moment('15/07/21-23:59:59', 'DD/MM/YY-HH:mm:ss');
momentDate.format('DD/MM/YYYY'); // => "15/07/2021"
momentDate.format(`yyyy-MM-dd'T'HH:mm:ss.SSSZ`); // => "2021-07-Th'T'23:59:59.000+07:00"
you can pass this string to a Date object as follow:
var date = new Date(YOUR STRING);
or you can use Date.parse() method if it does not work:
Date.parse('04 Dec 1995 00:12:00 GMT');
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 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');
In my php application I set the italian timezone like this way:
date_default_timezone_set('Europe/Rome');
the string above is located in my config.php file, the core of the app. Anyway, from the backend I using momentjs with CodeIgniter framework. When a user select a date from the properly input set this result:
Now I get the value from this input like this:
var end_date_temp = Date.parse($('#end-datetime').val());
And the initial result is wrong:
Tue Mar 01 2016 11:03:00 GMT+0100 (ora solare Europa occidentale)
The rest of code is:
var end_date = moment(end_date_temp).add(serviceDuration, 'minutes').format('DD/MM/YYYY HH:mm');
$('#end-datetime').val(end_date);
NB: I also tried to set moment.locale('it') but the same result appear, in my javascript libraries I've the italian timezone of momentjs. What is wrong?
UPDATE Code:
var end_date_temp = moment($('#end-datetime').val())._i;
var end_date = moment(moment(end_date_temp).add(serviceDuration, 'minutes')).format('DD/MM/YYYY HH:mm');
$('#end-datetime').val(end_date);
// parse the input string to a moment object, **specifying the input format**
var end_date = moment($('#end-datetime').val(), 'DD/MM/YYYY HH:mm');
// manipulate it as desired
end_date.add(serviceDuration, 'minutes');
// format it to the specified output format, and assign the result back to your field
$('#end-datetime').val(end_date.format('DD/MM/YYYY HH:mm'));
You can do this in one line of code if you like.
$('#end-datetime').val(moment($('#end-datetime').val(), 'DD/MM/YYYY HH:mm').add(serviceDuration, 'minutes').format('DD/MM/YYYY HH:mm'));
The locale setting isn't important with this particular bit of code, because you don't use any locale-specific functions or format specifiers
After a lot of attempts, I fixed in my timezone (italian) like so:
moment.locale('it');
var end_date_temp = moment($('#end-datetime').val()).format('DD/MM/YYYY HH:mm')
var end_date = moment(end_date_temp).add(30, 'minutes');
$('#end-datetime').val(moment(moment(end_date).toDate()).format('DD/MM/YYYY HH:mm'));
I declared the .locale as it, in the next time I formatted the time returned from the input text in my timezone as well. I've manipulated the date with the .add method, for do this I've create another instance of momentjs object. At the end I pass the value from the input, re-formatted the date again in my timezone format 'cause in the manipulation I lose the previous formatting. The final result is what I wanted. Hope this help, anyway, if someone find a solution more optimized than my I'll be happy to see.
momentjs is using the american date format (MM/DD/YYYY), enforce a format to get the right date:
var input = '03/01/2016 11:00';
var date = moment(input, 'DD/MM/YYYY HH:mm');
document.write(date);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.11.0/moment.min.js"></script>
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();