Javascript date formats customizing - javascript

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

Related

How to set a date string to a the following format

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

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

How to get date format in Angular-JS

My requirement is something different. I want to get the date format, not to format the date. Means I have a date string and now I want to get the date format of that date and apply it to the another date as a format.
Let me explain in brief with example:
var dateStr = "2015-06-06T12:00:00Z";
var d = new Date(dateStr);
here my date format is yyyy-MM-ddTHH:mm:ssZ you can see in dateStr object.
Now i will create another date and want to apply the same date-format to this new date.
var formatStr = "yyyy-MM-dd'T'HH:mm:ss'Z'"; // want to get this from above date, not hard coded like this.
var newDate = $filter('date')(d, formatStr);
here you can see that i have hard coded the format string, which i don't want to do. Here this string should be come from the above d date/or dateStr String.
You can do it by using momment.js
http://momentjs.com/downloads/moment.js
van date=new Date(date);
var dateInFormate=moment(date);
var date=dateInFormate.format('yyyy-MM-ddTHH:mm:ssZ');
As #Rob said, there is doubt on the reliably for all formats. What you need is pre defined map with key being the format and value being its corresponding regular expression.
Now, create a function with input as dateStr and will return the format. Like
function getDateFormat(dateStr) {
var format = default_format;
// Check in map for format
// If you get the format in map, return that else return a default format.
return format;
}

Convert plain string date into required format and difference between two dates

I am getting date from XML but date is in plain string format. I would like to create the difference from today's date and time and the date and time which i am getting from xml.
For example i am getting date as a plain string in this format (2012-10-17T08:15:19.500-05:00).Now when i am doing difference with current date&time than i need to display something like this "2:hr,32min".
Any help/suggestion would be a great input.
Thanks
This should work:
var myDate = new Date( '2012-10-17T08:15:19.500-05:00' ),
newDate = new Date();
Browser results can vary when parsing dates. I have a test Fiddle here.
To get the difference between dates: var diff = myDate - newDate; and to convert that back to something useful: Convert time interval given in seconds into more human readable form

How to get the date object

My value contains "08.07.1987", how to retrieve the date object for this string. new Date(val) gives correct date object values only for the string value that contains "/" format. can any one let me know hot to create date object for the values which contains "." or "-". in its format.
How about just adjusting the string to suit your needs?
var date1 = new Date("08.07.1987".replace('.','/'));
var date2 = new Date("08-07-1987".replace('-','/'));
You will need to be careful when asking Javascript to interpret a date in this format. As you can probably imagine, a date listed as "08.07.1987" doesn't really specify whether it's August 7th or July 8th.
In general, your best bet will be to specify a date format and parse accordingly.
you have to split the string into tokens for month date and year and then create it using JS Date API.
var date="08.07.1987";
var newDate = date.replace(/(\.|-)/g,"/"));
var dateObject = new Date(newDate);
Replace the delimiters?
var dateStr = "08.07.1987",
dateObj = new Date(dateStr.replace(/[-.]/g,"/"));
Of course you can encapsulate that in a function if need be...
try this new Date("08.07.1987".replace('.','/','g')); tested on firefox only

Categories

Resources