Transform format of date [duplicate] - javascript

This question already has answers here:
How to get date in UTC format irrespective of current system date with javascript?
(4 answers)
Closed 2 years ago.
I have following date Thu Apr 02 2020 11:57:21 GMT+0200 (Středoevropský letní čas). I need to compare this date with the date that is stored in API. The problem is that the date in API is in this format 2020-06-27T12:34:00.000Z.
Is there any way I can transform the format of the first date to be the same as the second date? Or can I comprare them in some way, that the formats wont matter?
Thank you in advance.

Use Date.prototype.toISOString()
The toISOString() method returns a string in simplified extended ISO format (ISO 8601),
which is always 24 or 27 characters long (YYYY-MM-DDTHH:mm:ss.sssZ or ±YYYYYY-MM-DDTHH:mm:ss.sssZ, respectively).
The timezone is always zero UTC offset, as denoted by the suffix "Z".
const time = 'Thu Apr 02 2020 11:57:21 GMT+0200 (Středoevropský letní čas)'
const result = new Date(time).toISOString();
console.log(result);

You can do this
const date = new Date("Thu Apr 02 2020 11:57:21 GMT+0200 (Středoevropský letní čas)");
const formattedDate = date.toISOString(); // This the formatted date

Related

How to convert UTC to local date in JavaScript [duplicate]

This question already has an answer here:
Convert UTC date received from server to local timzone date in Javascript
(1 answer)
Closed last year.
I want to convert 2022-01-15T12:22:24.0078237 into Sat Jan 15 2022 23:24:18 GMT+1100. The answers in Convert UTC date time to local date time say that the Date constructor should convert from UTC to local date, but clearly this isn't working here. What can I do to convert the UTC date into the local client date?
new Date("2022-01-15T12:22:24.0078237")
> Sat Jan 15 2022 12:22:24 GMT+1100 (Australian Eastern Daylight Time)
new Date()
> Sat Jan 15 2022 23:24:18 GMT+1100 (Australian Eastern Daylight Time)
Add 'Z' to the date string, like this:
const dateString = "2022-01-15T12:22:24.0078237";
const localDate = new Date(dateString + "Z");

How do I convert ISO date format to yyyy-MM-dd format using momentjs or vanilla JS? [duplicate]

This question already has answers here:
How do I format a date in JavaScript?
(68 answers)
Closed 4 years ago.
I'm trying to hit a soccer sports API which includes date in the format of yyyy-mm-dd, only the scores from that date to current date will be displayed. The current date is chosen by user using a calendar but when the user chooses the date from calendar, it gets displayed in ISO format as "Fri Aug 17 2018 00:00:00 GMT +0545 (Nepal Time)" . I want to convert this date in the front end in the yyyy-mm-dd format and send it to the API Url in back end. I'm using AngularJS and Java. How do I convert the full ISO date into that format?
Based on that output it sounds like your date is stored as a JavaScript date object (see: https://www.w3schools.com/js/js_dates.asp)
To get the string you want, one solution would be to take the value of your input (I'll call it d) and do the following (I assume you have momentjs loaded:
var datestring = moment(d).format('YYYY-MM-DD')
datestring should now include the date in the format you want... if for some reason d is a string instead of a date object, you can create a parsing pattern following the momentjs doc here: https://momentjs.com/docs/#/parsing/
Assuming you have a JavaScript Date object to work with, you can do this in plain JS:
var datestring = dateobj.toISOString().substring(0, 10); // 'yyyy-MM-dd'
If you only have the display string ("Fri Aug 17 2018 00:00:00 GMT +0545 (Nepal Time)"), you can first convert that into a Date object with this:
// displaystring = "Fri Aug 17 2018 00:00:00 GMT +0545 (Nepal Time)";
var dateobj = new Date(displaystring);
...and then do the datestring conversion above.

new Date('dd/mm/yyyy') instead of newDate('mm/dd/yyyy') [duplicate]

This question already has answers here:
Why does Date.parse give incorrect results?
(11 answers)
How to convert dd/mm/yyyy string into JavaScript Date object? [duplicate]
(10 answers)
Closed 4 years ago.
Is it possible to enter date in dd/mm/yyyy format into newDate (e.g. 03/01/2018) so that it returns object Wed Jan 03 2018 00:00:00 GMT+0000 (Greenwich Mean Time) {}?
If I have a date 03/01/2018 it returns Thu Mar 01 2018 00:00:00 GMT+0000 (Greenwich Mean Time) {} instead... I'm not trying to pass mm/dd/yyyy format..
I've looked at plenty of posts on this and can't find an answer to my question.
You have no control over the Date constructor, so you need to feed it a date in the format that it wants. Since you are formatting the date yourself, it is better to use the other Date constructor, which takes the year, monthIndex, and day as arguments, since it is more bullet-proof across different browsers and runtimes:
function my_date(date_string) {
var date_components = date_string.split("/");
var day = date_components[0];
var month = date_components[1];
var year = date_components[2];
return new Date(year, month - 1, day);
}
console.log(my_date("03/01/2018"));
The case of dates one area where I install the moment library on nearly every JavaScript project I create.
Note: Snippet may display the result differently; check your console.
You could use regex like so:
var date = new Date("03-01-2018".replace( /(\d{2})-(\d{2})-(\d{4})/, "$2/$1/$3"))
I suggest you use momentjs from http://momentjs.com/ . it is very easy to use to output any format u want.
moment().format('MMMM Do YYYY, h:mm:ss a'); // June 28th 2018, 10:30:09 pm
moment().format('dddd'); // Thursday
moment().format("MMM Do YY"); // Jun 28th 18
moment().format('YYYY [escaped] YYYY'); // 2018 escaped 2018
moment().format();

how to format Date object so that it gives me yyyy-MM-dd hh:mm:ss [duplicate]

This question already has answers here:
Where can I find documentation on formatting a date in JavaScript?
(39 answers)
Closed 5 years ago.
I have Date object and would like it formated.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
This is the format I would like to be able to print
yyyy-mm-dd hh:mm:ss
2017-08-09 19:48:54
Whatever I try I can't get it to using console.log to print that. I allways get something like
Wed Aug 09 2017 19:48:54 GMT+0200 (Central Europe Daylight Time)
or
Wed, 09 Aug 2017 17:48:54 GMT
I do not need all that extra information embedded. If I create Date object and time is 19:53:00 and date is 2017-08-09 then I need just this 2017-08-98 19:53:00
How do I get that exactly? I need to send this to server just like that.
This works fine :
function T(n) { return n<10?'0'+n:n; }
var d = new Date();
var formatted = d.getFullYear()+'-'+T(d.getMonth())+'-'+T(d.getDay())+' '+T(d.getHours())+':'+T(d.getMinutes())+':'+T(d.getSeconds());
console.log(formatted); // => 2017-07-03 19:08:30
let d = new Date(); // prints Wed Aug 09 2017 13:55:59 GMT-0400
d.toISOString(); // prints "2017-08-09T17:55:59.428Z"
// process the string to output in your desired format
You could use momentjs library:
> moment().format('YYYY-MM-DD HH:mm:ss')
< "2017-08-09 01:57:51"
You can also use moment with a javascript Date object:
const myDate = new Date()
moment(myDate).format('YYYY-MM-DD HH:mm:ss')

Converting Javascript Date

I have a date String in this format: Tue Sep 02 00:00:00 GMT+200 2014, I'd like to have only in Javascript this ISO Format: 2014-09-02T00:00:00.000Z.
So I have wrote this code:
var date = new Date("Tue Sep 02 00:00:00 GMT+200 2014");
date.toJSON();
but it returns: "2014-09-01T22:00:00.000Z".
How can I have the right date in ISO format? Thank you.
If you want to use the JavaScript native Date Object, you may want to look at its documentation first, especially the toISOString() method.
var date = new Date("Tue Sep 02 00:00:00 GMT+200 2014");
var n = date.toISOString();
this returns:
n: '2014-09-01T22:00:00.000Z'
which is the right ISO format. Your initial time is GMT+2 so, in ISO time, it corresponds to the same date/time but two hours before. As it is the 2nd of Sept, 00:00:00, 2 hours before lead to the day before, the 1st of Sept, at 22:00:00. You can't have the 2014-09-02T00:00:00.000Z you want in your question because it is not corresponding to an ISO date.
You can read more about ISO 8601 on Wikipedia.
There is a method for that.
date.toISOString()

Categories