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
Related
I'm working on a program that takes input from a user for a time. The program will take the information and automatically generate a Unix Timestamp using the current date as the date in the timestamp.
For example:
Daniel wants to generate a Unix Timestamp for 8:30 AM on Christmas
Day. He runs a command /unix 8:30, and the console prints out
1640421000.
What's the best way to achieve this? I understand how to generate a Unix Timestamp, but how do I edit just the time to the user input. Any help would be greatly appreciated.
You can create a Date for the current date, set the time as required, then generate a time value in seconds.
If the user will always enter H:mm and you don't need to validate the input, then the following will do:
let time = '8:30';
let tv = new Date().setHours(...(time.split(/\D/)), 0) / 1000 | 0;
// Check value
console.log(new Date(tv * 1000).toString());
However, the input should be validated (e.g. hours 0-23, minutes 0-59).
I just went with the following:
const time = interaction.options.getString('time');
const date = new Date().toISOString().slice(0, 10);
console.log(Math.round(new Date(`${date} ${time}:00`).getTime()/1000));
Seems to work for me.
We escape of using moment now and use date-fns instead. In some places we still use moment on front.
Example of code on server
//date in yyyy/mm/dd format in query params
startOfDay = StartOfDay(new Date(date));
return startOfDay
And when I display this date on front, she changes to local timezone(-4 hours). If i use date-fns-tz and convert to Canada timezone, I will get date with -4 hours and after display -4 hours more. How to resole this issue? I need to add 4 hours to date for my current timeZone. My utcOffset = 4.
Although I am not sure what function your are currently using from date-fns-ts, what has worked for me is to use utcToZonedTime(). This function gets
a date/time in the local time of any time zone from UTC time
import { utcToZonedTime } from 'date-fns-tz'
const now = new Date() // UTC
const nowCustomTimeZone = utcToZonedTime(now, 'Europe/Amsterdam')
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
trying to format utc time from server in time ago using moment.js fromNow but in some occasions I get "in 5 hours" instead.
timestamp from a server - 2017-11-29T15:03:21
var utcTime = new Date(timestamp);
var timeAgo = moment(utcTime).fromNow();
console.log(timeAgo)
all dates are in past so how can I fix this so I dont get time in a few hours ?
If you want "2017-11-29T15:03:21" treated as UTC, you can either use moment's utc method or just append a "Z" to the string. Since you're already using moment.js, it's more reliable to parse it with moment.js than the built-in parser:
var timestamp = "2017-11-30T00:20:48";
// Append Z
console.log(moment(timestamp + 'Z').fromNow());
// Use .utc
console.log(moment.utc(timestamp).fromNow());
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.19.3/moment.min.js"></script>
You need to tell moment that this date is in UTC using moment.utc
var utcTime = new Date(timestamp);
var timeAgo = moment.utc(utcTime).fromNow();
If you don't, moment assumes this date is in your local timezone (which I can tell is Eastern Standard Time by the offset).
In your local timezone, this date is actually 5 hours in the future. Only in UTC is it a few seconds ago, because your local timezone is 5 hours behind UTC.
As per documents https://momentjs.com/docs/#/displaying/fromnow/
you can customize the locale https://momentjs.com/docs/#/customization/relative-time/
As default locale future time will be future: "in %s", having in which is as per documents. if you want to change it then update the locale and use as you want.
Hope this helps
Hello guys I was wondering how do I convert a date into Unix time stamp using the library moment.js so I can compare the oldDate with another date.
This is what I tried:
var oldDate = (moment.unix(1490632174)).format();
// here I got the Date in string format
var newDate= moment.utc('2017-03-27T18:29:59+02:00', "YYYY-MM-DD");
// now I want to convert it again into unix timestamp and I don't know how to do it.
console.log(oldDate, newDate);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
Documentation is a wonderful thing. Unix Timestamp (seconds)
moment().unix();
moment#unix outputs a Unix timestamp (the number of seconds since the Unix Epoch).
moment(1318874398806).unix(); // 1318874398
This value is floored to the nearest second, and does not include a milliseconds component.
var oldDate = moment.unix(1490632174).unix();
// here I got the Date in string format
var newDate= moment.utc('2017-03-27T18:29:59+02:00', "YYYY-MM-DD");
// now I want to convert it again into unix timestamp and I don't know how to do it.
console.log(oldDate, newDate.unix());
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
You can use the < and > operators:
var oldDate = moment.unix(1490632174);
var newDate= moment.utc('2017-03-27T18:29:59+02:00', "YYYY-MM-DD");
console.log(oldDate<newDate, oldDate>newDate);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
Another way you could approach this is to leverage the native Date.parse("2017-03-27T18:29:59+02:00 GMT") method in Javascript. This method parses a date string and returns Unix Time in ms.
For more info, checkout Date.parse() documentation and this Unix Time Converter that makes use of both moment and the native JS method.