I get a different date - javascript

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/

Related

How to convert a german date string into a date correctly?

I have a string, which I want to convert to a Date;
let dateStr = "01.04.1990"
let date = new Date(dateStr);
but if I try to console log the date I get Thu Jan 04 1990 00:00:00. As you see day and month are switched but why?
How would I convert that string correctly?
You could reorder the values for an ISO date string and get the instance with this value.
let dateStr = "01.04.1990"
let date = new Date(dateStr.replace(/(.*)\.(.*)\.(.*)/, '$3-$2-$1'));
console.log(date);
In genereal Date.parse() is expecting an ISO-8601 formatted date string.
A recommendable approach would be to use a library like Luxon, as suggested here: stackoverflow

JavaScript input date convertion

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.

Momentjs return wrong date after minutes manipulation

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>

JS: Convert Today's Date to ISOString() with Fixed Time

I'm trying to convert today's date to an ISO standard string but with the fixed time of T00:00:00.000Z.
I can get as far as returning a ISO string of today's date and time:
var isoDate = new Date().toISOString();
// returns "2015-10-27T22:36:19.704Z"
But I wanted to know if it's possible to have a fixed time, so it should return:
"2015-10-27T00:00:00.000Z"
Is this possible?
Any help is appreciated. Thanks in advance!
To get the current UTC date at midnight:
var d = new Date();
d.setUTCHours(0);
d.setUTCMinutes(0);
d.setUTCSeconds(0);
d.setUTCMilliseconds(0);
var output = d.toISOString();
To get the current local date, with the time portion set to UTC midnight:
var d = new Date();
var ts = Date.UTC(d.getFullYear(), d.getMonth(), d.getDate());
var output = new Date(ts).toISOString();
As for which to use, think through your requirements very carefully, The current UTC date and the local date may indeed be two different days.
For example, when it's midnight (00:00) October 27th in UTC, it's 8:00 PM on October 26th in New York.
Also, consider using moment.js, which makes operations like either of these much easier with the startOf('day') and .utc() functions.

Use JavaScript to convert a date string with timezone to a date object in local time

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();

Categories