I create a date with Luxon in the following way:
const date = DateTime.utc(2000, 6, 23).toFormat('yyyy-MM-dd')
Then I try to convert it to a JS date by doing this:
const jsDate = new Date(date)
Finally, I convert it back to the 'yyyy-MM-dd' format with
const parsedDate = DateTime.fromJSDate(jsDate).toFormat('yyyy-MM-dd')
But instead of giving me 2000-06-23 it gives me 2000-06-22. Is this a bug on Luxon or I need to do something to get the correct date?
I try to convert it to a JS date by doing const jsDate = new Date(date)
Uh, why not just date.toJSDate()?
Instead of giving me 2000-06-23, DateTime.fromJSDate(jsDate).toFormat('yyyy-MM-dd') gives me 2000-06-22. Is this a bug on Luxon or I need to do something to get the correct date?
According to its docs, fromJSDate defaults to creating a DateTime instance in the local (system) timezone. If you expect another UTC date back, you need to specify that:
DateTime.fromJSDate(jsDate, {zone: 'utc'}).toFormat('yyyy-MM-dd')
Related
I have a date string const someDate = 2023-02-13T09:00:00.000-05:00
The problem is when I'm formatting it via dayjs
dayjs(someDate).format('h:mm A')
It returns me string according to my local timezone, when I need to keep like I received.
Any way to disable converting time to local timezone in dayjs?
Yes!
You can disable converting to local timezone in dayjs by passing in the original timezone as an additional argument in the dayjs constructor.
Example:
const someDate = "2023-02-13T09:00:00.000-05:00";
const originalTimezone = someDate.slice(-6);
const formattedDate = dayjs(someDate).utcOffset(originalTimezone).format('h:mm A');
The utcOffset() method allows you to set the offset in minutes from UTC for a specific date instance. The originalTimezone constant is used to extract the timezone offset (-05:00) from the original date string someDate, and pass it to the utcOffset() method. This will ensure that the formatted date stays in the original timezone.
I'm using devExtreme dxScheduler and i'm trying to display meetings after fetching them from api, the problem is that i can't recreate the original date format ("YYYY-MM-DDThh:mm:ssZ") since i'm getting the dates as timestamp.
Here is how it's stores :
var startDate = moment("2021-05-24T16:30:00.000Z").valueOf()
// 1621873800000
Here is what i'm trying to do to recreate the format:
var startDate = moment(new Date(startDate)).format("YYYY-MM-DDThh:mm:ssZ")
//"2021-05-24T07:30:00+03:00"
Notice that the original date ("2021-05-24T16:30:00.000Z") and the formatted date ("2021-05-24T07:30:00+03:00") are different ...hence the calendar do not displays them.
Looks like the date is being converted into your local timezone, thus the difference. You may need to add Moment Timezone to be able to get the timezone back in to recreate it to the format you need. Also consider adding utc() before the format to bring it to Zulu time.
Fix 1
I see from the DevExtreme page that it needs to be displayed within this format:
currentDate: new Date(2021, 4, 27)
Maybe you need to format it before adding it like this:
var check = moment("2021-05-24T16:30:00.000Z", 'YYYY/MM/DD');
var month = check.format('M');
var day = check.format('D');
var year = check.format('YYYY');
console.log(month,day,year);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>
And then in your dxScheduler add the property like this:
currentDate: new Date(year, month, day);
Fix 2
If that's not the problem, you can install moment-timezone
var a = moment.utc("2013-11-18 11:55").tz("Asia/Taipei");
var b = moment.utc("2013-11-18 11:55").tz("America/Toronto");
a.format(); // 2013-11-18T19:55:00+08:00
b.format(); // 2013-11-18T06:55:00-05:00
a.utc().format(); // 2013-11-18T11:55Z
b.utc().format(); // 2013-11-18T11:55Z
In this example, you first create moment.utc("2013-11-18 11:55") object in UTC, and then change its timezone to specified. This also works if you create the object in your default timezone: moment("2013-11-18 11:55").
Note that created moments have equal UTC time because these moments were created in a default timezone.
Turns out that displaying a calendar event with DevExtreme requires only to use regular date object.... so no need to do anything spacial.
I've a moment object to which I'm trying add some days.
However, it is returning the same moment object as a result.
But, if I simply try to do it on the current date, it works fine.
Also, note that, I always receive a moment object to which I need to add the days.
Code:
const someDate = moment('22-03-2020');
console.log(someDate.add(5, 'days');
someDate is something I receive from the server and is always a moment object.
How do I fix this?
You should specify what format you used, like so:
const someDate = moment('22-03-2020', 'DD-MM-YYYY')
const newDate = someDate.add(5, 'days')
console.log(newDate)
To format a moment object, you just have to add .format() like so:
console.log(newDate.format())
"Deprecation warning in moment js - Not in a recognized ISO format"
if you provide not valid ISO format for more check here information
Deprecation warning in moment js - Not in a recognized ISO format
// Recommended format: YYYY/MM/DD
const someDate = moment('2020-03-22');
console.log(someDate.add(5, 'days').format("YYYY-MM-DD"));
Test here
I have this application where I want to use you date, but the problem is that the date is not working as I expect.
I create a date object like this:
// Get today's date
today: function () {
// Create a new date
var date = new Date();
// Set to midnight
date.setHours(0, 0, 0, 0);
// Return our date
return date;
},
and If I output that date in my view I get yesterdays date at 23:00 hours....
Which looks like this:
2015-07-08T23:00:00.000Z
Does anyone know how I can get the date to be formatted properly?
Update
Just to elaborate a bit, I want to use the date to compare against records in the database. These records have the date applied to them, because the JavaScript is showing the local date time, it is not comparing correctly. Also there is a case where I am saving that date and I don't want it to save the local date.
based on your culture setting you can use the
date.toLocaleDateString()
this will give localized string format back
date.toUTCString();
date.toLocaleString();
date.toLocaleDateString();
date.toDateString();
date.toISOString();
Find your answer here :) And the best option is to use momentjs http://momentjs.com/
So, I ended up creating this function:
// Converts a date to a timeStamp
this.convertToTimeStamp = function (dateTime) {
// Get just the date
var date = dateTime.toDateString();
// Get the timestamp
var timeStamp = Date.parse(date);
// Return our timeStamp
return timeStamp;
};
If my understanding is correct, that should create the same date no matter what timezone / locale you are in.
How can i change the current date to this format(DD/MM/YYYY) using moment.js?
I have tried below code.
$scope.SearchDate = moment(new Date(), "DD/MM/YYYY");
But it's return 0037-11-24T18:30:00.000Z. Did't help to format current date.
You need to call format() function to get the formatted value
$scope.SearchDate = moment(new Date()).format("DD/MM/YYYY")
//or $scope.SearchDate = moment().format("DD/MM/YYYY")
The syntax you have used is used to parse a given string to date object by using the specified formate
You can use this
moment().format("DD/MM/YYYY");
However, this returns a date string in the specified format for today, not a moment date object. Doing the following will make it a moment date object in the format you want.
var someDateString = moment().format("DD/MM/YYYY");
var someDate = moment(someDateString, "DD/MM/YYYY");
This worked for me
var dateToFormat = "2018-05-16 12:57:13"; //TIMESTAMP
moment(dateToFormat).format("DD/MM/YYYY"); // you get "16/05/2018"
This actually worked for me:
moment(mydate).format('L');
for anyone who's using react-moment:
simply use format prop to your needed format:
const now = new Date()
<Moment format="DD/MM/YYYY">{now}</Moment>
A safe way to do this
moment.locale('en-US');
moment().format("L");
"06/23/2021"
moment.locale('fr');
moment().format("L");
"23/06/2021"