How to set a date string to a the following format - javascript

I'm using javascript and trying to convert a string to a different format. I did some research on this format but no one has asked about it yet so I thought I may ask. So I have seen that others want to convert a date string that has a shorter length. But this format is different. The format I have now by doing this:
const now = new Date();
const currentDate = now.toISOString();
I get this number:
2021-12-05T07:52:47.485Z
However, I want to make it the format like this:
2021-12-05T00:00:00.000+00:00
Is there any way to do so? I don't see others asking about this so not sure if possible

Use moment library:
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment.min.js"></script>
Like this:
var now = new Date();
now.setHours(0,0,0,0);
var format = 'YYYY-MM-DD[T]HH:mm:ss.SSS[+00:00]';
console.log(moment(now).format(format));
Link: https://codepen.io/sdssz1365/pen/rNGORKR

Related

How to convert a date string from 'dd//mm/yy' to 'mm/dd/yy' in javascript?

I am using a library to calculate the age from date of birth. I am taking date of birth as an input which is in the format of dd/mm/yy but the library that calculated the age accepts it in the format of mm/dd/yy. One solution to this is to change the date selector format in the application but I dont want to do that since it gets confusing.
I searched the solution on stackoverflow but couldnt find the solution here- How to format a JavaScript date
How about a simple split and join:
var yourDate = "10/12/2021";
var arrayOfDate = yourDate.split("/");
console.log([arrayOfDate[1], arrayOfDate[0], arrayOfDate[2]].join('/'));
Just split it with / and then use destructure it and get the desired result as:
const result = `${mm}/${dd}/${yy}`;
var oldDate = "10/12/21";
var [dd, mm, yy] = oldDate.split("/");
const newDate = `${mm}/${dd}/${yy}`;
console.log(newDate);

Javascript date formats customizing

I'm trying to test an API to check if the dates are present in the comment. The date is given in ISO format in the comment like 2020-02-18 21:30:13
When I compare with the Date() and convert it to ISO format, the format is slightly different from my date which makes my test to fail. How do I make the format the same as the one is my API response?
Below is my code:
var dateobj = new Date();
var B = dateobj.toISOString();
pm.test("Comment has Date", function (){
pm.expect(responseBody.split("*/")[0]).to.include(B)
})
Something Like This?
Javascript is not very flexible with Dates. But I think creating a formatting function shouldn't be a problem at all, try this:
var date = new Date();
var formattedDate = (date)=>{
return (`${date.getFullYear()}-${date.getMonth()}-${date.getDay()} ${date.getHours()}:${date.getMinutes()}:${date.getSeconds()}`);
}
alert(formattedDate(date));

Momentjs get date and set format

for a weight tracking project i want to get a date from a user in his specific format eg:"05-12-19" , and i want to format it with momentjs to the standard javascript format
This code below is what i tried and think is the nearest to the result i want:
let newDate = moment().format("05-12-19","DD-MM-YYYY");
console.log(newDate); //05-12-19
the result that i was expecting is 05-12-2019 but got something different take a look here (trying to meet stack-overflows quality standards lol)
To create your date, something like this:
let newDate = moment("05-12-19","DD-MM-YY");
console.log(newDate.toDate());
to output your desired format
let newDateStr = moment("05-12-19","DD-MM-YY").format("DD-MM-YYYY");
console.log(newDateStr);
after looking for a while i found a similair answer here.
it wasn't exactly what i was looking for so i'll post here my complete answer:
let newDate = moment("05-12-19", "DD-MM-YY").format("DD-MM-YYYY");
console.log(newDate);
in the moment function the first argument is my date, the second argument is the format of this date, because momentjs doesn't know this format. in the format function i enter the date i want it to format to.

Date format change To UTC

I have getting a dateformat like '23.10.2017'. I need to format this in to
'10/23/2017'
I just tried
var crDate='23.10.2017';
var newDateF=new Date(crdate).toUTCString();
but it showing InvalidDate
can anyone help to change the format.
Thanks in advance
I don't think using Date() is the solution. You can do
var crDate = '23.10.2017';
var newDateF = crDate.split(".");
var temp = newDateF[0];
newDateF[0] = newDateF[1];
newDateF[1] = temp;
newDateF.join("/");
This splits the string into an array, swaps the first and second elements, and then joins back on a slash.
A regex replacement will do the trick without any Date functions.
var date = '23.10.2017';
var regex = /([0-9]{2})\.([0-9]{2})\.([0-9]{4})/;
console.log(date.replace(regex,'$2/$1/$3'));
Just use moment.js if you can :
// convert a date from/to specific format
moment("23.10.2017", "DD.MM.YYYY").format('MM/DD/YYYY')
// get the current date in a specific format
moment().format('MM/DD/YYYY')
Moment is a very usefull and powerfull date/time library for Javascript.

Google Apps Script - Using replaceText to convert date format

I get a batch report with an odd date format of dd.mm.yyy and I would like to automatically be able to convert them all to something google understands is a date, like mm/dd/yyyy. Any help would be awesome. I am a n00b with regex.
function myFunction() {
var doc = DocumentApp.getActiveDocument();
var text = doc.editAsText();
// Change up the date format
text.replaceText("c?c.c?c.cccc", "/");
}
you could split the date based on the delimiter and then mash them back together how you want with something like this:
function myFunction() {
text = '12.03.012'
textArray = text.split('.')
text = textArray[0]+'/'+textArray[1]+'/2'+textArray[2]
Logger.log(text)
}
Logging Output shows:
12/03/2012
I would use momentjs for that. That way you can go directly to a javascript date object, and don't have to mess around with regExps. You can use their parsing string-format api to convert your format to standard.
var legitDate = moment(oddlyFormatedDate, "MM-DD-YYYY"); // use MM DD etc to describe your odd date format.

Categories

Resources