I have the following date string: 2022-04-16T18:31:00+02:00 and I would like to extract the local time i.e. 2022-04-16T18:31 and offset information +2:00 or 2 using date-fns.
I know I can do it with a simple string manipulation like this:
const [dateTime, offset] = "2022-04-16T18:31:00+02:00".split('+')
However, if possible, I would like to avoid string manipulation and use the library instead.
I can see not many people is answering here so, here is how you can get date and time in Javascript with this kind of string
const [dateTime, offset] = "2022-04-16T18:31:00+02:00".split('+');
var newDate = dateTime;
//above variable newDate will hold value that is => 2022-04-16T18:31:00
//don't use split('+'); so you can get Timezone as well
getTimeZone = "2022-04-16T18:31:0002:00";
var getTimeZone = getTimeZone.substring(19, getTimeZone.length-0);
console.log("timezone is "+ getTimeZone);
`````
I think you should look from date-fns-tz.
Check the method (utcToZonedTime, getTimezoneOffset).
Hope this help 👋🏽
You can try something like this... [untested code]
import { format } from "date-fns";
const d = Date.parse("2022-04-16T18:31:00+02:00");
const outputDate = `${format(d, "yyyy-MM-dd'T'HH:mm")}`;
console.log(outputDate);