I want to convert datetime like '2015-05-01 05:13:43' into timestamp.
Is there Any way to do it using JavaScript?
With pure JS you can try with:
new Date(Date.parse('2015-05-01 05:13:43+0000')).getTime() / 1000
It's important to add +0000 at the end of the string - otherwise browser will use your local timezone and add/remove few hours from the result.
getTime method gives you time in ms - so we have to divide it by 1000.
You can do it!
let dateToConvert = '2015-05-01 05:13:43'
let date = new Date(dateToConvert)
let timestamp = date.getTime()
Related
I have a time say for eg : 10:59:02 Now i need to add 2 minutes here & it should give me time as 11:01:02. I tried but was not able to achieve Can anyone help me with this ?
Try using moment library.
var date = moment('10:59:02 PM', 'hh:mm:ss A')
.add(2, 'minutes')
.format("hh:mm:ss");
console.log(date);
<script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.7.0/moment.min.js" type="text/javascript"></script>
Actually, you need to convert the time/date in Unix timestamp then you can add the minutes and then again pass that timestamp in Date constructor. you get the time as you needed.
var unixTimestamp = Math.round(new Date("2019-05-22 15:10:00").getTime()/1000); // convert to unixtimestamp
console.log(unixTimestamp);
unixTimestamp += 120 // added 2 min
date = new Date(unixTimestamp * 1000); // again convert to date
console.log(date); // got your solution.
I am using the Persian date picker and get the Unix time with it
and just want to convert to Gregorian.
here's my code:
persian date picker Unix time => 1532967741167
let unix = 1532967741167
let date = moment.unix(unix).format("YYYY-MM-DD");
console.log(date);
heres what i get from moment => 50547-10-25
here's what I get from epochconverter.com
and it's correct
any idea what's going on in here ?! :|
The problem is that you have to divide by 1000
let unix = 1532967741167/1000
this happens beacause as momentjs docs implies
To create a moment from a Unix timestamp (seconds since the Unix
Epoch), use moment.unix(Number). This is implemented as
moment(timestamp * 1000), so partial seconds in the input timestamp
are included.
here is the link
How can I get the timestamp after manipulating days in Moment.js?
I tried use:
var a = moment().subtract(10, 'days').calendar().getTime()
to get the timestamp, but failed.
It is not really clear what you mean by "timestamp".
To get the number of milliseconds since the Unix epoch, use moment().valueOf();. This corresponds to JS Date.getTime();.
To get the number of seconds since the Unix epoch, use moment().unix();.
To get the hour/minute/second as numbers, use moment().hour(); / moment().minute(); / moment().second();.
To get the ISO 8601 string (recommended for sending data over the wire), use moment().toISOString(); (e.g. "2013-02-04T22:44:30.652Z").
To print a user readable time, use moment().format();, e.g. moment().format('LTS') will return the localized time including seconds ("8:30:25 PM" for en-Us).
See moment.js - Display - Format for format specifiers.
Classic calendar formatting:
const daysBefore = moment().subtract(10, 'days').calendar();
for unix timestamp (current date minus 10 days):
const daysBefore = moment().subtract(10, 'days').unix();
Just remember, apply formatting after subtraction, as formatting returns a string, not momentjs object.
You can use moment().toDate() to get a Javascript Date object. Then you can get the timestamp with getTime().
calendar does not have method getTime(). You can format your time as follows:
let format = 'YYYYMMDD';
let a = moment().subtract(10, 'days').format(format);
console.log(a)
Try the following to get the timestamp:
moment().subtract(10, 'days')._d
try this one
moment().subtract(10, 'days').calendar().format('HH:MM:SS'); //get datetime
moment().subtract(1, 'days').calendar(); // Yesterday at 1:16 PM
I need to get the difference (in minutes) from a datetime that I get froma get request in a string format to now.
According to my research, I can use moment.js to do so, but I haven't figured out now.
That format I am getting the date/time to be compared is as:
2017-02-10T20:52:13.885Z
I have already tried to do some operations with moment.js such as
moment().startof(comparedTime).fromNow())
But it returns nothing.
What are the alternatives and the best way to do this?
Can't you just use vanilla javaScript?
var getDate = '2017-02-10T20:52:13.885Z'; //get time from server
var parseDate = new Date(getDate).getTime(); //change string into Date object into milliseconds
var nowDate = Date.now(); //get current Date in milliseconds
var minutes = Math.round((nowDate-parseDate)/1000/60); //subtract times, count seconds (/1000), count minutes (/60)
console.log(minutes);
You need to create a moment object by passing the date string in. e.g.
myDate = moment(myISOString)
https://momentjs.com/docs/#/parsing/
Then you can use the moment object as described in the docs.
With Moment.js, this is simply:
moment().diff('2017-02-10T20:52:13.885Z', 'minutes') // 65
If you want partial minutes included, then pass true as a third parameter:
moment().diff('2017-02-10T20:52:13.885Z', 'minutes', true) // 65.04565
I want to convert a string "2013-09-05 15:34:00" into a Unix timestamp in javascript. Can any one tell how to do that? thanks.
You can initialise a Date object and call getTime() to get it in unix form. It comes out in milliseconds so you'll need to divide by 1000 to get it in seconds.
(new Date("2013/09/05 15:34:00").getTime()/1000)
It may have decimal bits so wrapping it in Math.round would clean that.
Math.round(new Date("2013/09/05 15:34:00").getTime()/1000)
try
(new Date("2013-09-05 15:34:00")).getTime() / 1000
DaMouse404 answer works, but instead of using dashes, you will use slashes:
You can initialise a Date object and call getTime() to get it in unix form. It comes out in milliseconds so you'll need to divide by 1000 to get it in seconds.
(new Date("2013/09/05 15:34:00").getTime()/1000)
It may have decimal bits so wrapping it in Math.round would clean that.
Math.round(new Date("2013/09/05 15:34:00").getTime()/1000)
For this you should check out the moment.s-library
Using that you could write something like:
newUnixTimeStamp = moment('2013-09-05 15:34:00', 'YYYY-MM-DD HH:mm:ss').unix();
I would go with date parse myself since it is the native solution. MDN documentation.
const datetimeString = '04 Dec 1995 00:12:00 GMT';
const unixTimestamp = Date.parse(datetimeString);
// unixTimestamp = 818035920000