How to display properly time on client? [duplicate] - javascript

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

Related

How to covert date time to local date time using timezone string in JavaScript? [duplicate]

This question already has answers here:
Convert date to another timezone in JavaScript
(34 answers)
Closed 12 months ago.
I want to convert given Date time to local Date time, when input Date time and timeZone is given separately as below
Date -20220120080000 ('yyyymmddHHMMSS')
timezone string -("Australia/Perth")
How to convert above timezone to local Date time without using moment library ?
If you are able to use moment, this becomes pretty straightforward.
Check this working example.
let date = "20220120080000";
const format = "YYYYMMDDHHmmss";
let tz = "Australia/Perth";
const remoteTime = moment.tz(date, format, tz);
const localTime = moment(remoteTime).local();

mySql Timestamp Display Conversion [duplicate]

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}`}

Converting Date with Javascript [duplicate]

This question already has answers here:
Convert .NET date format into JavaScript date
(1 answer)
Parsing a Auto-Generated .NET Date Object with Javascript/JQuery
(2 answers)
Closed 2 years ago.
I'm attempting to convert a Date that is returning from the database as /Date(1570474808430)/. I'd like to convert it as a standard date time result such as it's appearing in the database as : 2020-08-04 11:08:22.630. Is this something that is possible? I'm not entirely sure why it appears as date time in the database but is returning as /Date(1570474808430)/ on the front end.
I've attempted to approach this with the following code:
let oldDate = /Date(1570474808430)/
let updatedDate = new Date(oldDate);
console.log(updatedDate)
My expected result is to convert /Date(1570474808430)/ to a date time: 2020-08-04 11:08:22.630
Your snippet, perhaps unknown to you, actually tries to pass a regular expression literal to the Date constructor. What comes back from your API is actually a string - of the form /Date(xxx)/ where xxx appears to be a Unix timestamp. In order to translate this to a Javascript date object, we need to parse the timestamp out of that - assuming the data always has this format - and ironically, the simplest way to do that is probably a regular expression:
const oldDate = "/Date(1570474808430)/";
const timeStamp = Number(oldDate.match(/\/Date\((\d+)\)\//)[1]);
const updatedDate = new Date(timeStamp);
console.log(updatedDate)
This works if you can guarantee your data will be in this form - but frankly it is not good code, and will certainly lead to problems if your data isn't always in this format. The best thing you could do, if it's possible, would be to update your data so that it holds a sensible date-string in a standard format. Or failing that, at least a number representing the Unix timestamp, without the /Date(...)/ cruft.

Getting NaN only on certain dates when converting with getTime() [duplicate]

This question already has answers here:
Why does Date.parse give incorrect results?
(11 answers)
Closed 5 years ago.
I'm working on a react app with firebase firestore for the db, the app stores a date field obtained from a datepicker in dd/mm/yyyy format as a string. I need to sort by dates and I'm converting dates to correctly sort by numbers with:
new Date(myDate).getTime()
But I'm getting NaN only on certain dates, for example:
01/12/2017 // 1484190000000
02/11/2017 // 1486782000000
22/08/2017 // NaN
Any idea???
var dateArr = '22/08/2017'.split('/');
new Date(dateArr[2], --dateArr[1], dateArr[0]).getTime();
Try this. 22/08/2017 - wrong format, need 08/22/2017

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