Convert date from local timezone? - javascript

The question is simple, I use momentjs to get the current date of the system, in particular this is the date returned:
Sun Jan 03 2016 17:17:00 GMT+0100 (ora solare Europa occidentale)
I want convert this date in this format like this:
03/01/2016 17:17:00
How I can achieve this?
This is my code:
moment(new Date()).toDate()

You can use momentjs's format() function to format the date. As an example:
moment.locale('it');
moment(new Date()).format('dddd MM/DD/YYYY h:mm:ss');
This returns the date as "Domenica 01/03/2016 10:17:49"

Related

Issue with Date formats conversion

I have the following date format : 2020-09-25T11:09:00.422Z, which in order to be processed, needs to be modified to the following format: Wed Apr 01 2015 00:00:00 GMT+0100
How to achieve that in a one liner ?
Thanks
Create a Date instance and pass the value as string:
new Date("2020-09-25T11:09:00.422Z")

How to convert new Date() to UTC date using moment JS

I want to convert the returned value of new Date to UTC format using moment js.
I tried using moment.utc but
moment.utc(moment(new Date())).format();
output -> 2020-01-01T04:00:00Z
but I want output in this format
Thu Jun 04 2020 00:00:15 GMT+0530 (India Standard Time)
just like what new Date returns but it should be in UTC.
and the other problem is that the return type of format is of
TYPE : STRING I want that the format should be the same but its type should be of date object.
Any help would be appreciated.

moment.js returning wrong day for a date

I have a field with a value of 03-01-2020 as a date in DD-MM-YYYY date format. When I use
moment.utc(document.getElementById('date').value, "DD-MM-YYYY HH:mm:ss").toDate()
and the value turns out to be a day behind as shown below:
Thu Jan 02 2020 17:00:00 GMT-0700 (Mountain Standard Time)
Is there anything I am doing wrong?
Changed the line to the following:
moment(document.getElementById('date').value + " 12:00", "DD-MM-YYYY HH:mm:ss").utc().format("YYYY-MM-DD HH:mm")
Based of this answer: moment.js - UTC gives wrong date

When I try to create a date object from another date format, the result date is changing it's value

When I try to create a date object from another date format, the result date is changing it's value. How to achieve this without changing the date value ?
new Date("Mon, 31 Oct 2016 00:00:00 GMT");
the result coming as Sun Oct 30 2016 20:00:00 GMT-0400 (Eastern Daylight Time), How can I get the Monday 31 date from the above?
Adjusting the timezoneOffset from the created date object should do the trick,
but be cautious while using it , as you should be sure that the date object was created from GMT not from some local time .
And the below answer has been posted assuming the input date was in GMT
var tempDate = new Date("Mon, 31 Oct 2016 00:00:00 GMT");
var tempTime = tempDate.getTime() + (tempDate.getTimezoneOffset() * 60000);
tempDate = new Date(tempTime);
console.log(tempDate);
It doesn't change the date, it just converts it to your local timezone. This is a bit of annoying behaviour and the only way I know to get around it is to set your system timezone to GMT. If you need to do date and time work, you might want to look at Moment.js - http://momentjs.com/

How to change the date string to date time with local timezone in js?

I use date picker and i get the value of the date picker as
document.getElementById("id_of_datepicker")
and when i change this to date using
new Date(document.getElementById("id_of_datepicker"));
it returns
Thu Feb 06 2014 05:30:00 GMT+0530 (IST)
and what i look for is instead of passing the dates as i do i need to send it in local timezone with 00 hours, min, secs which then has to be converted to utc. how can i do this?
please help me.
you have to split it manually and use Date.UTC for utc function like this:
var dateStr = document.getElementById("id_of_datepicker");
var dateArr = dateStr.split(/[\-T:]/);//suppose it is like 2014-06-12T00:00:00
var localTime=new Date(Date.parse(dateArr[0],dateArr[1],dateArr[2]), 0, 0, 0)
var utcTime=new Date(Date.UTC(dateArr[0],dateArr[1],dateArr[2]), 0, 0, 0)
You could try using moment.js for date/time manipulation
Suppose this the dt value returned by the date object
Thu Feb 06 2014 05:30:00 GMT+0530 (IST)
Date dt=new Date();
dat=dt.getFullYear()+'-'+dt.getMonth()+'-'+dt.getDate();
It will return date dt print in yyyy-mm-dd format ot you can change in your desired format

Categories

Resources