How to convert one time format to Date javascript? - javascript

I have this weird format of dates 1640828010231 which I'm trying to convert to actual date using javascript. I'm using the following code.
new Date('1640828010231')
but it is giving me the current time. I have no idea what format that date is in. I have tried various ways. please help.

It's timestamp,
Have a look to mozilla doc to understand better https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date#the_ecmascript_epoch_and_timestamps

Related

How do I convert a date retrieved by API to a date understood by React

In my app, I retrieve time from API in a format like this : 2020-08-31T15:05:10.768904Z and I'm trying to compare this date with the current Date().
What is the best way to calculate difference between the two.
Thanks!
Sorry, that's not in comment because I cant yet. You have to decompose it and i would use regex to get it.

Converting time-stamp to milliseconds in javascript

I have a date which is of format 20170622T164322Z. How to convert it into milliseconds(epoch time) in javascript. I am new to javascript, kindly help
If you could use moment.js in your project, you can do this:
moment("20170622T164322Z").unix()
Example running on the console of the browser:
moment("20170622T164322Z").unix()
1498149802
Btw, moment.js is just great when dealing with date time.

String to Date mongodb using node.js and moment.js

In my application i allow user to set a date using a date-picker component.
My issue is when trying to convert string date to UTC/ISO date format the conversion is not happening properly, see the below example.
Example:
User Selected String Date : 01/09/2015 (DD/MM/YYYY)
While im using moment.js to convert the above date to native JS date, See below:
moment(req.body.datePicker,'DD/MM/YYYY')
All good till here, but when the data stored in db the date is getting reduced by 1 day. I have no idea where its getting messed up.
I have managed to create a re-producable scenario using jsfiddle-example please have a look for a better understanding.
My assumption:
when i see the db collection the date is getting stored with a default time: 18:30:00.00Z
this could be one of the reason why the dates are getting changed.
Your problem is timezone.
In my case 01/09/2015 gives 2015-01-08T23:00:00 which is only -1 hour since my timezone is GMT+1
Finally after doing some research i myself found the solution or a hack to fix this issue, it may not be the correct way of doing it but for the current scenario it worked like a charm.
Solution:
All i did was just created a full ISO/UTC date string hard-coding the time to zero. moment(req.body.datePicker,'DD/MM/YYYY').format('YYYY-MM-DD HH:mm:ss.000').toString()+'Z' by doing this the default time will always set to Zero and since the string is a fully qualified ISO/UTC standard format, mongoDB will not try to alter it.
Working example:
http://jsfiddle.net/r42jg/1004/
If any one has a better solution, the please post here so we can improvise this better.

Find time difference between current time and a string

how am I able to parse a time string in the following format 12:10AM and then get javascript to find the time difference between that time and the current time
Use momentjs. It can parse, manipulate, and display dates in javascript. It's incredible and has wide variety of parse formats and manipulations.
If you don't find the format available, just use Regex and extract the time components and pass them to momentjs. Then you can find the time difference and format the difference in displayable format also.
For more info on momentjs: http://momentjs.com/
You may use javascript regular expressions:
var dateParseRe = /(\d\d):(\d\d)(\w\w)/g;
dateParse.exec("12:10AM");

How to display the date example format " 15-Apr-2012 10.12 AM" exactly using this javascript new Date() format in javascript

I need to display the date format like this 15-Apr-2012 10.12 AM.I tried to use time=new Date() using javascript.But i couldnt get the correct date format .it shows the GMT global timing.I need to display the time format.please anyone can know?
Take a look at DateJS. If you're planning to do more stuff with dates than just formatting it, I would strongly suggest it. Definitely makes things easier with manipulation and displaying.
Easiest thing to do is use a library: try Moment, it is the real deal.

Categories