How to convert datesrting to short date object in javascript? [duplicate] - javascript

This question already has answers here:
How do I format a date in JavaScript?
(68 answers)
Closed 2 years ago.
I am new to UI (User Interface) coding and I have date from json as "2021-02-28 00:00:00". But while writing to the xlsx I don't want the date in string form. And this is what I have tried.
variable = new Date("2021-02-28 00:00:00")
Which give the date object as below
Sun Feb 28 2021 00:00:00 GMT+0530 (India Standard Time)
But I want the date to be in below format [should be still a date object not a string]
28 Feb 2021 00:00:00

you can do it using Date object but there's many lines to do that.
I handle all my date formatting with moment.
e.g.
moment('2021-02-28 00:00:00').format('DD MMM YYYY hh:mm:ss')
console.log(moment('2021-02-28 00:00:00').format('DD MMM YYYY HH:mm:ss'))
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>

Related

Transform format of date [duplicate]

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

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

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