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;
}
Related
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));
I have a string of Date and Time ("2017-11-29 11:08:43" YYYY-MM-DD hh:mm:ss) like this. I want to convert it into "29-11 11:08"(DD-MM hh:mm) format.
I tried it using below code. But not get any success. have you any solution?
convert: function (idleFrom) {
var date = Ext.Date.parse(idleFrom, "Y-m-d");
return date;
}
first change your string into date format using
var dt = new Date(idleFrom)
than change into you required format using
Ext.Date.format(dt, 'm/d/Y');
follw this link for more format
Hope it will work :)
If you have a string, and want it reformatted, you have to parse the string into a JS date object first, and then format the JS date object into the string representation you need:
var date = Ext.Date.parse("2017-11-29 11:08:43", "Y-m-d H:i:s")
var str = Ext.Date.format(date, "m/d/Y")
Please note that Ext.Date.parse is really picky regarding the format identifier. If the matching between the format identifier and the input string's format is not 100%, your date will be null.
E.g. Ext.Date.parse("2017-11-29 11:08:43", "Y-m-d H:i") will be null because the seconds are in the date string, but missing from the format identifier.
I want to convert systematic date into readable date format. However when I pass systematic date as argument to date constructor I get Invalid date response. How to do this properly in order to display formatted date such as dd-mm-yyyy for GMT+2 ?
var date = message.date; // => 1466663308000
var dateObject = new Date(date);
console.log(dateObject);
Console output:
Invalid Date
You have to make sure the timestamp value is a number and not a string:
var date = message.date;
var dateObject = new Date(+date); // note the +
console.log(dateObject);
Once you've got a valid date, there are many other questions here about formatting dates.
I tried the code, it is absolutely correct.
I can get the correct date
var d=new Date(1466663308000);
document.write(d);
But I tried another way:
var x = "1466663308000";
var d=new Date(x);
document.write(d);
I got the "Invalid date", so I guess, message.date should be a string, please try to long(message.date).
Hi I have use the string object for represent the date in the following format
var date="18/01/2011";
var dateFormat="dd/MM/yyyy";
Note:
In my scenorio i have use the dd/MM/yyyy format;
dateFormat will be different in my client side.
how to convert these date default JavaScript dateFormat as MM/dd/yyyy in Generic way.
I have tried in by split date by and swap the month ,date to achieve this requirement. But in my client side i dont know about the format of the date how to convert any other format to default Javascript format
Hope this helps:
var date="18/01/2011";
var parts = date.split('/');
var result = new Date(parts[2], parts[1], parts[0]);
var datestring = "2014-02-27:04:05";var d = Date(datestring);
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