This question already has answers here:
how to format date in Component of angular 5
(4 answers)
Closed 3 years ago.
I wanna change the input date with time and time zone to this format yyyy-MM-dd+timezone
Example:
I have this input
Sat Oct 05 2019 00:00:00 GMT+0200 (Ora legale dell’Europa centrale)
I want an output like this
2019-10-05+2:00
Is there a simple way to convert this?
You can do this with simple Date functions.
public transform(date: Date): string {
return `${date.getFullYear()}-${date.getMonth()+1}-${date.getDate() >= 10 ? date.getDate() : '0'+date.getDate()}${date.getTimezoneOffset()}`;
}
Related
This question already has answers here:
toISOString() return wrong date
(3 answers)
Format JavaScript date as yyyy-mm-dd
(50 answers)
Closed 4 months ago.
Here is my Cypress JS code:
cy.get('creationDateElement').then(date => {
const reformattedDate = new Date(date.text())
cy.log('Reformatted Date: ' + reformattedDate)
cy.log('ISO string: ' + reformattedDate.toISOString().split('T')[0])
})
I am trying to transform the date so that it returns 2015-09-11
Here is what is logged with the above code:
Reformatted Date: Fri Sep 11 2015 00:00:00 GMT+0100 (British Summer Time)
ISO string: 2015-09-10
As you can see, the day being returned is 10 rather than 11, but I don't know why this is happening.
Can someone please tell me why this is happening, & how I can return 2015-09-11 instead?
The code is working with the below dates though, so I don't know why the one above is causing an error:
Reformatted Date: Wed Feb 28 2007 00:00:00 GMT+0000 (Greenwich Mean Time)
ISO string: 2007-02-28
This is part of a Cypress UI test. So the actual date comes from an element on the UI that contains a date:
This question already has answers here:
How to initialize a JavaScript Date to a particular time zone
(20 answers)
Why does Date.parse give incorrect results?
(11 answers)
Closed 4 years ago.
I'm using javascript new Date format. It is working well in android and windows but when I use it on iphone it outputs different result. How can I be able to solve the problem?
var rawGetDate = "2019-02-01 00:00:00";
var dateF = new Date(rawGetDate.replace(' ', 'T'));
console.log(dateF);
OutPut in Androaid: Tue Jan 01 2019 00:00:00 GMT+0600
OutPut in Iphone: Mon Dec 01 2018 00:00:00 GMT+0500
This question already has answers here:
Why does Date.parse give incorrect results?
(11 answers)
Javascript: parse a string to Date as LOCAL time zone
(3 answers)
Closed 5 years ago.
I'm getting crazy with showing a date correctly to the client:
From the API I receive data.expiry_date and its value is: 2017-09-06T23:59:59Z
The client to show that data looks like this:
var date = new Date(data.expiry_date);
$('#expiry_date').val(`${date.getDate()}/${date.getMonth() + 1}/${date.getFullYear()}`);
The result is a day before than expected: it should be 06/09/2017 but it show 07/09/2017.
Basically the date value from the action var date = new Date(data.expiry_date); is: Thu Sep 07 2017 01:59:59 GMT+0200 (CEST).
How can I get rid of it?
You're not in the same time zone.
2017-09-06T23:59:59Z is UTC
which is same as
Thu Sep 07 2017 01:59:59 GMT+0200 (CEST)
This question already has answers here:
How do I format a date in JavaScript?
(68 answers)
Convert PHP Unix Timestamp to Javascript Timestamp format
(1 answer)
Closed 6 years ago.
I initialize my timestamp variable like
var current_time = new Date();
My output is in
Tue Oct 11 2016 15:09:04 GMT+0800 (Malay Peninsula Standard Time)
format. How can I change it to 2016-10-11 07:19:48pm format?
you can acheive this using moment.js lib,
just run
moment().format('YYYY-MM-DD hh:mm:SS A'); // print 2016-10-11 03:20:88 PM
This question already has answers here:
How do I format a date in JavaScript?
(68 answers)
Closed 6 years ago.
I create this date object:
var date = new Date(06, 2016);
When I open browser watch to check the created object I see this:
Mon Jun 06 2016 12:51:11 GMT+0300 (Jerusalem Daylight Time)
How can I simplify the above date object to this:
2016-06-06
You could do this...
date.toISOString().split("T")[0] // "2016-06-06"