Javascript change the data in some data - javascript

I have a variable that's returning the following content:
{
"id":1,
"refnumber":3,
"active":false,
"date":"2017-09-14T23:00:00.000Z",
"addons":0,
"age":0
}
If you look at the data field I need a way of changing the data on that field to yyyy-mm-dd.
In other words .. if we take this example as a reference I would need the data field value:
This:
2017-09-14T23:00:00.000Z
To be changed to this:
2017-09-14
How can I do this?

You should just be able to slice away the trailing characters.
data.date = data.date.slice(0, 10);
This works because your data format is in a predictable form where the month and day have a 0 padding.

I recommend a library called momentjs for all your javascript date manipulation needs.
https://momentjs.com/
With it, you can do what you want easily: moment(data.date).format('YYYY-MM-DD')

Note that your JSON is invalid because the date string is not properly quoted, but assuming it's corrected and that your JSON object is named myObject, the following would work:
var date = new Date(myObject.date); //your string will become a date object
//now operate on that date object by extracting the year, month,
//and day into a string
console.log(date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate());

Well you could convert the string to date and then format it
Here is an example
function formate(date) {
if (typeof date == "string")
date = new Date(date);
var day = (date.getDate() <= 9 ? "0" + date.getDate() :
date.getDate());
var month = (date.getMonth() + 1 <= 9 ? "0" +
(date.getMonth() + 1) : (date.getMonth() + 1));
var dateString = day + "-" + month + "-" +
date.getFullYear() + " " + date.getHours() + ":" +
date.getMinutes();
return dateString;
}

Related

How to convert string date to following date format dd-mm-yyyy as a object type in javascript

How to convert string date to following date format dd-mm-yyyy as a object type in javascript
Ex:
var date='2021-02-23T10:08:44'
var convertedDate='02-23-2021'
The below code works and converts as string '02-23-2021', but because of this I am not able to sort it in UI according to the year,month and date since I am sending it as a string.
let convertedDate = (((date.getMonth() > 8) ? (date.getMonth() + 1) : ('0' + (date.getMonth() + 1))) + '-' + ((date.getDate() > 9) ? date.getDate() : ('0' + date.getDate())) + '-' + date.getFullYear().toString().substr(-4));
Here I have more elaborated question:
Sort by last updated entry in descending order as default using react-bootstrap-table-next
You can use this:
var strDate = '2021-02-23T10:08:44'
var convertedDate = new Date(strDate).toLocaleDateString('US').replaceAll('/', '-')
console.log(convertedDate) // 2-23-2021

How to format existing date using JavaScript/jQuery

I am trying to format date time using JavaScript/jQuery but it's not happening. My code is below.
<div id="divID"></div>
<script>
var formatDate = function(date){
return date.getDate() + "/" + date.getMonth() + "/" +date.getYear() + " "+ date.getHours() + ":" + date.getMinutes() + ":" + date.getMintutes() + ":" + date.getSeconds();
}
var timestamp="2016-12-16 07:58:30 AM ";
var date= new Date(timestamp);
document.getElementById('divID').innerHTML = formatDate(date);
</script>
Here I have the existing time 2016-12-16 07:58:30 AM and I need change it to 16-12-2016 07:58:30 AM but here I could not get the proper output.
Your code has a few issues:
You have a syntax error, you're calling getMintutes()
You appear to be attempting to show the minutes twice, so you can remove one of those calls
getFullYear() fits your needs better than getYear()
You should use - not / to delimit the date values.
You can add AM or PM to the end of the string by checking if hours < 12
Your timestamp string isn't valid. It should not contain 'AM' or 'PM' - hence why the code doesn't work in Firefox.
With that in mind, try this:
var formatDate = function(date) {
return date.getDate() + "-" + date.getMonth() + "-" + date.getFullYear() + " " + ('0' + date.getHours()).slice(-2) + ":" + ('0' + date.getMinutes()).slice(-2) + ":" + ('0' + date.getSeconds()).slice(-2) + ' ' + (date.getHours() < 12 ? 'AM' : 'PM');
}
var timestamp = "2016-12-16 07:58:30";
var date = new Date(timestamp);
document.getElementById('divID').innerHTML = formatDate(date);
<div id="divID"></div>
You could use a library to make the date formatting logic simpler, but it's rather wasteful to load an entirely library when a single line of code works fine.
The timestamp you are using will return an invalid date so you should remove the AM. Using moment.js you can do it like this:
var timestamp = "2016-12-16 07:58:30";
var formattedDate = moment(timestamp).format('DD-MM-YYYY h:mm:ss A');
console.log(formattedDate);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.5.1/moment.min.js"></script>
you can use a library named moment.js http://momentjs.com/
var date = new Date();
moment(date).format('DD-MM-YYYY HH:mm:ss A')
jQuery dateFormat is a separate plugin. You need to load that explicitly using a <script> tag.
You can use JQuery UI Datepicker for getting the formatted date like the following.
let myDate = '2020-11-10';
$.datepicker.formatDate('dd-M-yy', new Date(myDate));
The above code will return 10-Nov-2020.
You can get the desired output using JQuery UI Datepicker Widget as shown below.
var timestamp="2016-12-16 07:58:30 AM ";
var desiredTimestamp = $.datepicker.formatDate('dd-mm-yy', new Date(timestamp.split(' ')[0])) + ' ' + timestamp.split(' ')[1] + ' ' + timestamp.split(' ')[2];

change date format in displaying

I have this below code where i am trying to alert the date selected on click of an image. I am getting the alert but the issue is date format.
Below is alert i am getting. I need the alert in mm/dd/yyy.
And also why i am getting Thu Jan 01 1970 if i use todatestring() method.
Below is the javascript code
Try this function to return mm/dd/yyyy
function getmmDdYy(thisDate) {
return ('0' + (thisDate.getMonth() + 1)).slice(-2) +
'/' +
("0" + (thisDate.getDate())).slice(-2) +
'/' +
thisDate.getFullYear();
}
So on your fnUpdateDate(), do this:
function fnUpdateDate(date1,date2){
alert(getmmDdYy(new Date(date1)) +" ss "+ getmmDdYy(new Date(date2)));
}
Let me know if this works.
You can retrieve the single components from a date (day, month, year) and build a string with them (https://stackoverflow.com/a/30272803).
If you add the library moment.js you will be able to use formatting strings like YYYYMMDD (https://stackoverflow.com/a/27635223).
You can pass your date to this function and return the formate as per your need
function formatDate(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;
return (month+"/"+day+"/"+year);
}

Getting the date from Javascript comes up with bizarre values

I want to get the date and time in the following format:
yyyy.mm.dd.hh.mm.ss | 2014.11.6.20.31.24
However, my code (based on Get Current Time) is instead providing these values:
y??.m?.d?.hh.mm.ss | 114.10.4.20.31.24
Here is my code:
var dt = new Date();
var time = dt.getHours() + "." + dt.getMinutes() + "." + dt.getSeconds();
var date = dt.getYear() + "." + dt.getMonth() + "." + dt.getDay();
alert(date + "." + time);
Can someone please let me know why these odd values are in there 114.10.4 and how to change them to be what I want?
That is because you need to use
.getFullYear() for the full year
the .getMonth() is 0-based so you need to add 1
the function to get the day of month is .getDate(). The .getDay() is for the day of the week.
var dt = new Date();
var time = dt.getHours() + "." + dt.getMinutes() + "." + dt.getSeconds();
var date = dt.getFullYear() + "." + (dt.getMonth()+1) + "." + dt.getDate();
alert(date + "." + time);
If, for some weird reason, you are going only for firefox, you can use
var d = new Date(),
formatted = d.toLocaleFormat('%Y.%m.%d.%H.%M.%S');
alert(formatted);
Finally, you can use the great moment.js library and do
var formatted = moment().format('YYYY.MM.DD.HH.mm.ss');
You are using the wrong getters. Use getFullYear() instead of getYear(), and getDate() instead of getDay(). And add 1 to the month, because it starts at 0.
var dt = new Date();
var time = dt.getHours() + "." + dt.getMinutes() + "." + dt.getSeconds();
var date = dt.getFullYear() + "." + (dt.getMonth() + 1) + "." + dt.getDate();
alert(date + "." + time);
Just make sure that you are using methods what you want to use e.g:
dt.getYear() => dt.getFullYear()
For further reference see this.
should use getFullYear() instead of getYear() and getMonth() + 1 instead of getMonth() because it calculate form 0..11 and info about getDay()
var dt = new Date();
var time = dt.getHours() + "." + dt.getMinutes() + "." + dt.getSeconds();
var date = dt.getFullYear() + "." + dt.getMonth() + 1 + "." + dt.getDate();
alert(date + "." + time);
dt.getDay() this day of the week
The getDay() method returns the day of the week (from 0 to 6)
You need to use getDate() to know the number of the day (from 1 to 31)
Also, you need to add 1 to getMonth() because months in JavaScript starts on 0

Parsing the date in MM/DD/YY format

I get the response for the Date in this format while showing in the text box, how do i covert it to MM/DD/YYYY and Again re covert it to back to this format while sending
/Date(1306348200000)/
function dateToString(date) {
return (date.getMonth() + 1) + '/' + date.getDate() + '/' + date.getYear();
}
function dateFromString(str) {
return new Date(str);
}
Note, that month begins from 0.
To convert the regExp-like string to a real Date Object you could use:
var dateNum = Number('/Date(1306348200000)/'.replace(/[^0-9]/g,''))
, dat = new Date(dateNum); //=>Date {Wed May 25 2011 20:30:00 GMT+0200}
To display formatted dates I use my own small library, which may be of use to you.
var s = '/Date(1306348200000)/';
// convert to javascript date
var date = new Date(parseInt(s.substr(6, 13))); // removes /Date( & )/
// format the date
function pad(n) { return n < 10 ? '0' + n : n; } // leading zeros
var ddmmyy = pad(date.getDate()) + '/' + pad(date.getMonth() + 1) + '/' + date.getFullYear().toString().substr(2);
// convert back
s = '/Date(' + date.getTime() + ')/';
here you can find everything regarding javascript dates http://www.w3schools.com/js/js_obj_date.asp

Categories

Resources