I am getting the date as
data.created = "Wed May 03 2017 15:41:49 GMT 0530(IST)"
I want to convert it to
regular isi format like
2017-03-12
My code,
var created = new Date(data.created);
created = created.toISOString()
Can anyone help me.Thanks.
You can use Date $filter to get the formatted string.
var formatedStr = $filter('date')(new Date(data.created),'yyyy-MM-dd');
Since your input is URL encoded, first you have to decodeURIComponent (which replaces things like %20 with spaces and so on).
Then convert the result into a Date object.
Then use the getFullYear, getMonth and getDate functions to get these data, but don't forget to pad it with 0 (for numbers lower than 10).
var input = "Wed%20May%2003%202017%2015:41:49%20GMT%200530%20(IST)";
function formatDate(d) {
var month = (d.getMonth() + 1)
, day = '' + d.getDate()
, year = d.getFullYear()
;
month = month < 10 ? '0' + month : month;
day = day < 10 ? '0' + day : day;
return [year, month, day].join('-');
}
var decoded = decodeURIComponent(input);
var inputDate = new Date(decoded);
var output = formatDate(inputDate);
console.log(output);
Related
I am trying to put together a mostly automated form. I have the get current date fine but I am having problems collecting information from a user enter date to place it in another part of the form as well + 1 year. I.E. D.O.B = 08/06/2016 farther down the form expires 08/06/2017. I can make the current date enter automatically but when i try and get the date entered from the user nothing fills the lower date. I tried getting the date using document.getElementById('dob')
function datePone()
{
var date = new Date();
var day = date.getDate(document.getElementById('dob'))
var month = date.getMonth(document.getElementById('dob')) + 1;
var year = date.getFullYear(document.getElementById('dob')) + 1;
if (month < 10) month = "0" + month;
if (day < 10) day = "0" + day;
var oneYear = year + "-" + month + "-" + day;
document.getElementById("dateOneYear").value = oneYear;
}
I've tried using set date or making a new var using document.getElementById('dob') but nothing i have tried has worked so far.
Since "dob" is an input field, to the get the entered data you need to use value property, so try
document.getElementById('dob').value
Also I suggest using momentjs library to manipulate with dates.
Where you have:
var day = date.getDate(document.getElementById('dob'))
the getDate method does not take any arguments, so they are ignored and the above is equivalent to:
var day = date.getDate()
You don't specify what format you're using for the string, "08/06/2016" is ambiguous. Does it represent 8 June or August 6?
You should not use the Date constructor or Date.parse to parse date strings, write your own small function or use a library. Also, adding one year to a date like 29 Feb 2016 will end up on 1 March 2017, so you need to apply a rule to accept that or change it to 28 Feb 2017.
Anyhow, assuming the input is in the format dd/mm/yyyy and you want the output date in the format yyyy-mm-dd, you can use a library or small functions like the following:
// Parse string in d/m/y format
// If invalid, return invalid Date
function parseDMY(s){
var b = s.split(/\D/);
var d = new Date(b[2], --b[1], b[0]);
return d && d.getMonth() == b[1]? d : new Date(NaN);
}
// Return date string in yyyy-mm-dd format
function toISODate(d) {
return d.getFullYear() + '-' +
('0' + (d.getMonth()+1)).slice(-2) + '-' +
('0' + d.getDate()).slice(-2);
}
// Parse string to Date
var d = parseDMY('06/08/2016');
// Add one year
d.setFullYear(d.getFullYear() + 1);
console.log(toISODate(d));
Using a library like fecha.js, you'd parse the string using:
var d = fecha.parse('06/08/2016','DD/MM/YYYY');
and format the output:
fecha.format(d, 'YYYY-MM-DD')
I have an API result giving out timestamp like this 1447804800000. How do I convert this to a readable format using Javascript/jQuery?
You can convert this to a readable date using new Date() method
if you have a specific date stamp, you can get the corresponding date time format by the following method
var date = new Date(timeStamp);
in your case
var date = new Date(1447804800000);
this will return
Wed Nov 18 2015 05:30:00 GMT+0530 (India Standard Time)
Call This function and pass your date :
JS :
function getDateFormat(date) {
var d = new Date(date),
month = '' + (d.getMonth() + 1),
day = '' + d.getDate(),
year = d.getFullYear();
if (month.length < 2)
month = '0' + month;
if (day.length < 2)
day = '0' + day;
var date = new Date();
date.toLocaleDateString();
return [day, month, year].join('-');
}
;
In my case, the REST API returned timestamp with decimal. Below code snippet example worked for me.
var ts= 1551246342.000100; // say this is the format for decimal timestamp.
var dt = new Date(ts * 1000);
alert(dt.toLocaleString()); // 2/27/2019, 12:45:42 AM this for displayed
I need your help,
I can't seem to find any other help on this on the internet, because it seems its either one way ot the other. What I would like to be able to do is to create a combined, two-fold javascript function that would convert a long date string into the mm-dd-yyyy format, and when the same function is called again with no string specified to convert, to just return todays date in mm-dd-yyyy format.
Example:
getDate(Fri May 22 2015 13:32:25 GMT-0400)
would return: 05-22-2015
getDate()
would return today's date of 05-23-2015
Hi this should do the trick
FORMAT: mm-dd-yyyy
function addZeroIfRequired(dayOrMonth) {
return dayOrMonth > 9 ? dayOrMonth : "0"+dayOrMonth;
}
function getDate(dateString) {
var date = dateString ? new Date(dateString) : new Date();
return addZeroIfRequired((date.getUTCMonth()+1)) + "-" +
addZeroIfRequired(date.getDate())+ "-" +
date.getUTCFullYear();
}
console.log(getDate()); // 05-23-2015
console.log(getDate("Fri May 22 2015 13:32:25 GMT-0400")); 05-22-2015
NOTE: the +1 after the getUTCMonth().
JSFiddle. Open the console to see the result. https://jsfiddle.net/wt79yLo0/2/
ISO FORMAT: yyyy-mm-dd
Just in case someone is interested in the opposite format, the code would be much nicer and neater:
function getDate(dateString) {
var date = dateString ? new Date(dateString) : new Date();
return date.toISOString().substring(0, 10);
}
console.log(getDate());
console.log(getDate("Fri May 22 2015 13:32:25 GMT-0400"));
https://jsfiddle.net/wt79yLo0/
First I would recommend a very powerful library for JS called Moment.js which solves all this kind of problems.
But if you only want a snippet, here is my proposal:
function twoDigits(num) {
return ("0" + num).substr(-2);
}
function getFormattedDateDMY(date, separator) {
var day = twoDigits(date.getDate());
var month = twoDigits(date.getMonth());
var year = date.getFullYear();
return [day,month,year].join(separator);
}
function getFormattedDateMDY(date, separator) {
var day = twoDigits(date.getDate());
var month = twoDigits(date.getMonth());
var year = date.getFullYear();
return [month,day,year].join(separator);
}
console.log(getFormattedDateDMY(new Date(), "-")); //23-04-2015
console.log(getFormattedDateMDY(new Date(), "-")); //04-23-2015
With getDate(), getMonth() and getFullYear(). You have to set a "0" before the months and days which are < 10. GetMonth() starts with 0, therefore (getMonth() + 1).
function getFormattedDate() {
var date = new Date();
var day = date.getDate() > 9 ? date.getDate() : "0" + date.getDate();
var month = (date.getMonth() + 1) > 9 ? (date.getMonth() + 1) : "0" + (date.getMonth() + 1);
var year = date.getFullYear();
var formattedDate = day + "-" + month + "-" + year;
return formattedDate;
}
console.log(getFormattedDate());
Demo
I have a date at a string - dtStr
var dtStr = "Thu May 28 02:13:16 BDT 2015";
I want to get a date like - MM DD YYYY HH mm format from the dtStr. For getting this I am trying to convert the dtStr to a Date and then try to use date format like this -
var dtStr = "Thu May 28 02:13:16 BDT 2015";
today = new Date(dtStr);
alert( today.toLocalDateFormat("MM DD YYYY HH mm") );
But it didn't work for me. Can any one help me for - converting dtStr to a date with format MM DD YYYY HH mm ?
Thanks in advance.
Unfortunately, the string you're trying to parse won't be accepted by Date.parse(), the method that will parse the string when you're creating it. If the string will always be in that format, you could do some string manipulation and rearrange it to the RFC2822/IETF format, which Date() can handle.
// this creates a proper Date object
new Date("Thu, May 28 2015 02:13:16 +0600");
Alternatively, you could create a new Date object with one of the other constructors, by splitting/parsing the string yourself, and inserting them in the correct places in the constructor.
At this point, you'll have a Date object, but you still need to get the values from it - the only built in method that can do something like what you're trying to do is toLocaleFormat(), which isn't standard track (it's not supported in my version of Chrome, for example). Thus, you would need to get the values independently, and concatenate them together.
At this point, it's probably easier to just do straight up parsing of the string, and skip the Date object altogether, or use a library like datejs, which provides support for formatting output strings.
You may try to use the following method -
function formatReview(date){
/*****************************************************************
* The method parameter 'date' is in the following format -
* "Thu May 28 02:13:16 BDT 2015"
* Javascript 'new Date()' has not suitable constructor to
* support this format. The parameter 'date' need to
* convert to fee the Date() constructor.
*******************************************************************/
var monthSymbols = "JanFebMarAprMayJunJulAugSepOctNovDec";
var elements = date.split(" ");
var day = elements[0];
var monthName = elements[1];
var monthIndex = monthSymbols.indexOf(monthName)/3 +1;
var date = elements[2];
var year = elements[5];
var timestamp = elements[3];
var timestampElements = timestamp.split(":");
var hour = timestampElements[0];
var minutes = timestampElements[1];
var dateString = monthIndex +"-"+ date +"-"+ year +" "+ hour +":"+ minutes;
return dateString;
}
Try this
var todayDate=new Date("Thu May 29 2014 13:50:00");
var format ="AM";
var hour=todayDate.getHours();
var min=todayDate.getMinutes();
if(hour>11){format="PM";}
if (hour > 12) { hour = hour - 12; }
if (hour == 0) { hour = 12; }
if (min < 10){min = "0" + min;}
document.write(todayDate.getMonth()+1 + " / " + todayDate.getDate() + " / " + todayDate.getFullYear()+" "+hour+":"+min+" ");
fiddle: http://jsfiddle.net/e0ejguju/
I need to check if the date is in the past. This is what I have so far. JSfiddle here.
var date = "09/12/2013";
var d = new Date();
var month = d.getMonth() + 1;
var day = d.getDate();
var todaysDate = +(('' + day).length < 2 ? '0' : '') + day + '/' + (('' + month).length < 2 ? '0' : '') + month + '/' + d.getFullYear();
if (date < todaysDate) {
alert("in the past");
} else {
alert("in the future");
}
Currently it is saying that the date was in the past, when it should be in the future. I know I need to parse the string as a date, but not sure how.
Help?
With that input format, you can't use a string comparison, because the least significant values are on the left. Note: I'm assuing that date is December 9th, 2013. If you're doing the American thing where it's September 12th, 2013, you'll have to adjust the indexes into parts below.
You could reverse the fields:
var date = "09/12/2013";
var parts = date.split('/');
date = parts[2] + "/" + parts[1] + "/" + parts[0];
...and then do your string comparison (being sure to construct the string for "today" in the same order — year/month/day).
If you're going to do that, you could go ahead and finish the job
var date = "09/12/2013";
var parts = date.split('/');
var date = new Date(parseInt(parts[2], 10), // year
parseInt(parts[1], 10) - 1, // month, starts with 0
parseInt(parts[0], 10)); // day
if (date < new Date()) {
// It's in the past, including one millisecond ago
}
...but of course, if you don't want the expression to be true for one millisecond ago, your string approach is fine.
var date = new Date("09/12/2013");
var d = new Date();
console.log(date>d); // true
var date = new Date("09/12/2011");
console.log(date>d); // false
JavaScript's native Date comparator only works on Date objects, whereas you are comparing Strings. You should parse date into a Date object, and then compare it with d.
//define parse(string) --> Date
if(parse(date) < new Date()) {
alert('past');
} else {
alert('future');
}