let date = moment('08.01.2021').format('YYYY-MM-DD')
date is returned 2021-01-07 03:00:00 but i want to see date 2021-01-08 00:00:00.
How can I do that?
If you do not need Moment, you can use ISO at 15:00
const d = new Date("08.01.2021 15:00:00")
console.log(d.toISOString().split("T")[0])
Related
I'd like to get the Epoch timestamp of a date being entered by a user, but convert the time to 12:01 am in JavaScript.
How do I do that?
Take the timestamp and pass it while creating a instance of the Date class like:
let timestamp = 1629289414;
let dateInstance = new Date(timestamp * 1000);
console.log(dateInstance); // Wed Aug 18 2021 14:23:34....
From there on you have many ways to work with dateInstance. To get the 12:00am result from this it's more a string manipulation/adjusting thing.
Just check out the documentation on the the javascript Date instance: https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Date
If you have further questions just post the code you've written and maybe I can help you out
I have a string, which I want to convert to a Date;
let dateStr = "01.04.1990"
let date = new Date(dateStr);
but if I try to console log the date I get Thu Jan 04 1990 00:00:00. As you see day and month are switched but why?
How would I convert that string correctly?
You could reorder the values for an ISO date string and get the instance with this value.
let dateStr = "01.04.1990"
let date = new Date(dateStr.replace(/(.*)\.(.*)\.(.*)/, '$3-$2-$1'));
console.log(date);
In genereal Date.parse() is expecting an ISO-8601 formatted date string.
A recommendable approach would be to use a library like Luxon, as suggested here: stackoverflow
I have string:
date = "2019/1/16 00:00 +0900"
I'm in New York (timezone -5), I want to create an Date object like that:
Wed Jan 16 2019 00:00:00 GMT+0900
I can't use javascript to convert. It will return with timezone -5.
I use moment.js:
moment.tz(date, 'Asia/Tokyo').format('YYYY/MM/DD HH:MM');
However, it's not right. Could you please help me. Thank a lot.
It will work if your date object is a moment instance:
moment.tz(moment(date), 'Asia/Tokyo').format('YYYY/MM/DD HH:MM Z')
Timezones are difficult to get right. I think an easy-to-follow and somewhat idiomatic solution is this:
const date = "2019/1/16 00:00 +0900";
// parse in any timezone
const dateMoment = moment(date);
// deliberately set the timezone in which the moment is interpreted in
const timezonedMoment = dateMoment.tz('Asia/Tokyo');
// format the moment
const formattedDate = dateMoment.format('YYYY/MM/DD HH:MM z');
Of course, you would write this in a more concise form.
I need date format like : 2018-10-04T20:35:28. in javascript.
I don't know what format is this, but I already try follow
Now I have this:
var now = new Date();
var isoDate = new Date(now).toISOString();
My output is:
2018-10-05T04:55:58.896Z
But I have a wrong day because actual date is:
Thu 4 Oct 2018 22:56:53 CST
Why i have +1 day in all dates.
like #Nisarg Shah said "The ISO string is in UTC, the one in console is in your local time zone" . You can change it using this
new Date().toLocaleString("en-US", {timeZone: "America/New_York"})
Check this out for more information.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleString
var isoDate = new Date(now).toISOString();
// Output
2018-10-05T04:55:58.896Z
This isoDate is in UTC. You can see that there is a 'Z' in the end of the string. This means that the date is in UTC.
You can use Moment Timezone (moment.js) to convert any given date to another timezone.
moment.tz('2018-10-05T04:55:58.896Z', 'America/Toronto').format();
Just change the timezone name to the one you want to convert.
For Further Details
https://momentjs.com/timezone/docs/#/using-timezones/
I'm trying to convert today's date to an ISO standard string but with the fixed time of T00:00:00.000Z.
I can get as far as returning a ISO string of today's date and time:
var isoDate = new Date().toISOString();
// returns "2015-10-27T22:36:19.704Z"
But I wanted to know if it's possible to have a fixed time, so it should return:
"2015-10-27T00:00:00.000Z"
Is this possible?
Any help is appreciated. Thanks in advance!
To get the current UTC date at midnight:
var d = new Date();
d.setUTCHours(0);
d.setUTCMinutes(0);
d.setUTCSeconds(0);
d.setUTCMilliseconds(0);
var output = d.toISOString();
To get the current local date, with the time portion set to UTC midnight:
var d = new Date();
var ts = Date.UTC(d.getFullYear(), d.getMonth(), d.getDate());
var output = new Date(ts).toISOString();
As for which to use, think through your requirements very carefully, The current UTC date and the local date may indeed be two different days.
For example, when it's midnight (00:00) October 27th in UTC, it's 8:00 PM on October 26th in New York.
Also, consider using moment.js, which makes operations like either of these much easier with the startOf('day') and .utc() functions.