mySql Timestamp Display Conversion [duplicate] - javascript

This question already has answers here:
How do I format a date in JavaScript?
(68 answers)
Parsing a string to a date in JavaScript
(35 answers)
Closed 2 years ago.
I have a table where one of the columns is entered as CURRENT_TIMESTAMP.
I am pulling the data from the table and displaying it on my site. But can't figure out how to convert the date into a more reader friendly format.
Right now it looks like this: 2020-09-17T19:56:24.000Z (I also need to figure out how to convert to my timezone.)
But I want it to display like this: 09-17-2020 19:56 (would even prefer 07:56pm, but not critical)
I've seen a few examples where you can reformat it during the query. But all the examples I've seen are pulling just the date. I'm pulling everything. I'm fine with running a function on it after the query to convert... but I haven't been able to figure it out.

You could use a simple function using the built-in methods available to Date objects in js. Something like this is probably what you're looking for.
function formatDate(date){
const month = date.getUTCMonth()+1;
const day = date.getUTCDate();
const year = date.getUTCFullYear();
const hours = d.getUTCHours() > 12 ? d.getUTCHours() - 12 : d.getUTCHours();
const minutes = d.getUTCMinutes();
return `${month}-${day}-${year} ${hours}:${minutes}`}

Related

How to solve, in an elegant way and using JavaScript, the UTC problem, which subtracts a day when instantiating a Date in the format '2018-10-25'? [duplicate]

This question already has answers here:
Why does Date.parse give incorrect results?
(11 answers)
Closed 4 years ago.
I'm working on legacy system and in DB the birthday is coming this way '1992-05-18' in json. I am using AngularJS and when applying the data binding of this variable in an input type = "date", of an update form, it is necessary to instantiate a Date object. Like this:
//person.byrthday = '1992-04-26'
var person.birthday = new Date (person.birthday);
// after person.byrthday = '1992-04-25T00:00:00.000Z'
How can I solve this problem through Front End in an elegant way, without "breaking" two way data binding?
I find myself in Brasil UTC -03:00
There are a few ways to solve this problem. A quick and dirty solution could be to leverage moment.js. You can convert the response from the API to a true date format this way.
If you don't want to use an additionally library, you can make a function to parse the date string. You can do the following to parse is to become a correct date:
var dateSplit = person.birthday.split('-');
var mydate = new Date(dateSplit[0], dateSplit[1] - 1, dateSplit[2]);
person.birthday= mydate;
Take note that the month index starts at 0 (aka January=0). Hopefully this helps.

How could I validate/compare two dates in node js to ensure endDate is withing after 24hours of startDate? [duplicate]

This question already has answers here:
Node.js dates comparison
(3 answers)
Closed 4 years ago.
I have two dates i.e. startDate = 2018-11-14T08:55:03.021Z and endDate = 2018-11-14T09:13:03.097Z. How can I compare this two dates to ensure the endDate is within 24 hours after startDate. I want to add more validation like endDate should not be greater than currentDate. How could I do this? Is there any algorithm available?
It will be easier if you use some library to handle dates. I prefer using moment library for date related operations in JavaScript https://momentjs.com/docs/. You can use the code below to get the difference in hours using moment.
let momentStartDate = moment(startdate);
let momentEndDate = moment(endDate);
let duration = moment.duration(momentEndDate.diff(momentStartDate));
let differenceInHours = duration.asHours();
console.log(differenceInHours)

How to display properly time on client? [duplicate]

This question already has answers here:
Is there a simple way to convert a decimal time (e.g. 1.074 minutes) into mm:ss format using moment.js?
(4 answers)
Closed 5 years ago.
I send TimeSpan object called time from back-end to front-end.
Here how the type looks in back-end:
I have this type on client side:
I need to display time variable only hours:minutes:seconds.
How can I convert the type above(i.e. time) to format above?
var x = new Date(1561000); //replace with TotalMilliSeconds in your case
var time = x.toUTCString().split(' ')[4]; //slit by space character
console.log(time);

add days to current date in javascript [duplicate]

This question already has answers here:
How can I add 1 day to current date?
(10 answers)
Closed 5 years ago.
I am currently using this formula to populate the current date into a fillable PDF for work:
var f = this.getField("Today");
f.value = util.printd("mm/dd/yyyy", new Date());
This fillable PDF is a government document and once it's printed expires in 45 days including the date printed. Like a Temp Tag for a vehicle.
How do I add 44 days to this formula so that it automatically kicks out 45 days down the road?
Here is what you wanna do:
var laterDate = new Date(); // create new Date object set to current date
laterDate.setDate(laterDate.getDate() + 44); // add 44 days
then you could use your util library and display it:
util.printd("mm/dd/yyyy", laterDate);
For handling dates in JavaScript, you could use Moment.js. With moment you can perform a wide range of operations on dates and time. Check it's docs for more info. As for your query, you could do
moment().add(44, 'days');

Javascript backwards date, reverse? [duplicate]

This question already has answers here:
What is the best way to convert date from JavaScript string in format YYYYMMDD to JavaScript date?
(2 answers)
Closed 9 years ago.
does anybody know How I could reverse the date that I have in javascript?
I have a created date of files from google drive which I pulled using the google drive api, however the date is reverse.
2014-02-25
I would like, 25-02-2014 (dd/mm/yyyy)
var date = resp.createdDate;
date = date.substring(0, date.length - 14)
This is my code, i trimmed the end off, because it came with time etc.
my output is currently : 2014-02-25
Im unsure how I could do this and im sure its simple!
> "2014-02-25".split('-').reverse().join('-')
"25-02-2014"

Categories

Resources