How do I change the MYSQL timestamp date to the JS date using toISOString () and setting the time-zone to CET?
This is what I use and it returns the following format "2021-02-251 15:27:20" which is what I want, only the time should be +1 hour "2021-02-251 16:27:20 ":
registration_date_customer.toISOString().replace(/T/, ' ').replace(/\..+/, '')
The time on the database is correct (16:27:20).
Does anyone know how to set the CET time-zone?
You can use Date.localeString() to format in the correct timeZone, the IANA timezone "Europe/Paris" is equivalent to CET. Using the "sv" locale will result in an ISO formatted string (Sweden uses ISO date formatting).
const registration_date_customer = new Date("2021-02-25 15:27:20Z");
console.log(registration_date_customer.toLocaleString("sv", { timeZone: "Europe/Paris"}));
Related
From below code:
const dateString = '1994-09-15T12:00:00-03:00';
const parsedDate = parseISO(dateString)
const dateFormat = 'MM-dd-yyyy HH:mm:ss xxx'
console.log(format(parsedDate, dateFormat, { }))
I expect:
09-15-1994 20:30:00 -03:00 But i get 09-15-1994 20:30:00 +05:30 as my local timezone is +05:30
What am i missing here?
The parseDate() creates a date based on GMT, e.g. is adjusted by 3h based on your input '1994-09-15T12:00:00-03:00'.
The format() function with xxx formats local time without the Z, such as -08:00, +05:30, +00:00. If you want to format with GMT string, specify OOOO, which will give you something like GMT-08:00, GMT+05:30, or GMT+00:00, which again has the offset adjusted based on your browser's time zone.
See Time-Zones docs below if you want to format to a time zone other than your browser's one, such as GMT-03:00.
Docs:
ISO 8601: https://en.wikipedia.org/wiki/ISO_8601
parseDate(): https://date-fns.org/v2.29.2/docs/parseISO
format(): https://date-fns.org/v2.29.2/docs/format
Time-Zones: https://date-fns.org/v2.29.2/docs/Time-Zones
I have a date string of format dd.MM.yyyy HH:mm:ss but its UTC which I want to convert to local. How can I do it using moment library?
const dateStr = '20.09.2018 16:12:37';
const format = 'dd.MM.yyyy HH:mm:ss';
// for local time of UTC +3.00 new date Str will be 20.09.2018 19:12:37
You have to use moment.utc(String, String) to parse your string using UTC mode
By default, moment parses and displays in local time.
If you want to parse or display a moment in UTC, you can use moment.utc() instead of moment()
Then you can use local() to convert it to local time:
Sets a flag on the original moment to use local time to display a moment instead of the original moment's time.
Please note that since you input dateStr is not in ISO 8601/RCF 2822 recognized format you have to specify format when parsing it. Morover moment tokens are case sensitive so you have to use uppercase YYYY instead of yyyy to parse years and uppercase DD to parse day of the month since lowercase dd stands for day of the week (Mon, Tue, etc).
Here a live sample:
const dateStr = '20.09.2018 16:12:37';
const format = 'DD.MM.YYYY HH:mm:ss';
console.log(moment.utc(dateStr, format).local().format(format));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.1/moment.min.js"></script>
Have a look at Local vs UTC vs Offset guide to better understand how UTC and locale mode work in momentjs.
To convert from UTC to Local you need to use moment.local() method
const dateStr = '20.09.2018 16:12:37';
const format = 'DD.MM.YYYY HH:mm:ss';
var newDate = moment.utc().format(format);
var temp= moment.utc(newDate, format);
var local = moment(temp).local().format(format);
More info on the official Documentation
component:
pickupTime: string = ’ ’
this.pickupTime = moment().format(‘LT’);
console.log(‘pickup TIme’, this.pickupTime);
//display time in format 9:30 AM
or
pickupTime = new Date.toLocaleTimeString() //display time in format 9:30 AM
The time does not display in the pickupTime unless its formated in
toISOString()
example: pickupTime = new Date.toISOString()
In Ionic framework, datetime values as a string uses the standardized ISO 8601 datetime format. To format the current datetime to specific timezone, you are recommended to use moment.js, an open source javascript library. Please refer to this link in the documentation.
moment(new Date().toISOString()).locale('es').format();
I am using moment.js, and I want to convert from ISO Format date time to UTC time zone.
I can convert from local time to UTC+ISOformat, but I am not able to convert ISO format to UTC time zone.
Input:
2018-03-22T00:00:00Z
Expected output:
date should be in UTC time zone. If I calculate the it should be:
22018-03-21T18:30:00Z
First I want to convert into ISO, After that convert into UTC**.
Not able to Converted local date time to ISO then UTC
We can convert into string, But from ISO format can convert or not?
Fox example: I want to convert ISODATE(2018-03-22T00:00:00Z) into UTC time zone.
function toISO(dt) {
return moment(dt).format("YYYY-MM-DDTHH:mm:ss") + "Z";
}
var date = new Date();
var isoDate= toISO(date)
Direct we can convert
function toISOWithUtc(dt) {
return moment(dt).utc().format("YYYY-MM-DDTHH:mm:ss") + "Z";
}
var date = new Date();
toISO(date)
Both 2018-03-22T00:00:00Z and 2018-03-21T18:30:00Z already are in UTC.
Note the Z in the end? This means that the date is in UTC. It's not a hardcoded letter that you can just append to the end - it has a specific meaning, it tells that the date/time is in UTC.
If you want to convert that UTC date to another timezone (I'm guessing that's what you want), you can use moment.tz:
// convert UTC 2018-03-22T00:00:00Z to Asia/Kolkata timezone
moment.tz('2018-03-22T00:00:00Z','Asia/Kolkata')
Just change the timezone name to the one you want to convert.
And calling format() will give you the converted date/time in ISO8601 format:
moment.tz('2018-03-22T00:00:00Z','Asia/Kolkata').format() // 2018-03-22T05:30:00+05:30
Note that the offset changed from Z to +05:30. This means that the date/time above is 5 hours and 30 minutes ahead UTC, which is the result of converting 2018-03-22T00:00:00Z to Kolkata's timezone.
Note: I think you're mistaking the concepts here.
UTC is a time standard. It defines things like the current date/time accross the world, it uses atomic clocks to make sure everybody on Earth are synchronized, and timezones are all based on offsets from UTC.
ISO8601 is a standard for exchanging date/time-related data. It defines formats (text representations of dates). A date/time can be in ISO format, regardless of being in UTC or not.
That's why "convert from UTC to ISO" (or vice-versa) makes no sense at all.
I am having a problem with Moment.js.
Here are the codes.
var date = "2016-07-26 06:15 pm";
var unixDate = moment(date).unix();
var renderDate = moment.unix(unixDate).format("YYYY-MM-DD hh:mm a");
console.log(renderDate);
I want the value of "renderDate" to be same as the value of "date" variable. However, what gets printed out in the console is "2016-07-26 06:15 am".
I am completely lost as to why everything remains the same except "am/pm" at the end.
unix method accepts only number
You need to parse date before usage
moment('2016-07-28 06:15 PM', 'YYYY-MM-DD hh:mm a').format('YYYY/MM/DD hh:mm a')
Look at http://momentjs.com/docs/. You can only use the string without a format if it is in ISO 8601 format. In your example, it ignored the am/pm indicator and viewed the hour as being in 24-hour format.