How to simplify javascript date object? [duplicate] - javascript

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"

Related

Convert date to format yyyy-MM-dd+timezone [duplicate]

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

how to set time zone in javascript new Date() [duplicate]

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

How can we change the date format in JavaScript? [duplicate]

This question already has answers here:
How do I format a date in JavaScript?
(68 answers)
Format a date string in javascript
(7 answers)
Where can I find documentation on formatting a date in JavaScript?
(39 answers)
Closed 4 years ago.
Given the data 2018-04-28T18:38:23Z
When I use Date(2018-04-28T18:38:23Z)
This gives me Sat Apr 28 2018 18:38:23 GMT-0500
I want to remove that GMT thing, but how can I do with simply without
writing some long code to parse it out?
JavaScript Date has some methods you could use to craft the String you want.
Here's something close, you could play around with the methods (getHours/Minutes/Seconds), but if you want exactly that without the timezone, I would recommend parsing out or doing some String manipulation:
var d = new Date();
console.log(d.toDateString() + " " + d.toLocaleTimeString());
// Output:
// Sat Apr 28 2018 4:58:05 PM

Showing correct date from API [duplicate]

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)

Javascript Change timestap format [duplicate]

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

Categories