momentjs - change format of local time - javascript

I am using moment to get the time. I am getting it in format: Thu Feb 21 2019 10:44:21 GMT+0500 (Pakistan Standard Time). However, I need it in format 03:44:21PM. Can anyone please tell me how I can achieve that using momentjs?

You can use momentjs for this purpose. moment().format()
moment().format('hh:mm:ss A')
For more details visit docs
Local can also be used to convert out of a fixed offset mode:
moment.parseZone('2016-05-03T22:15:01+02:00').local().format('hh:mm:ss A');

Get your TimeZone and convert your timezone using this:
let timeZone = moment.tz.guess();
let date = 'Thu Feb 21 2019 10:44:21 GMT+0500' // input date (other timezone)
let convertDate = moment(date).tz(timeZone) //converted Time

Related

How to convert local date (without time) in UTC DateTime in JavaScript

I am trying to convert the local date (without time) in UTC Date and pass it to server. Suppose I have a local date selected from Calendar which is 01-Jun-2021 and when converted to Date its Tue Jun 01 2021 00:00:00 GMT+0530.
My requirement is that, it should be converted to Mon May 31 2021 18:30:00 GMT+0530 and when sent to the server it should be Mon May 31 2021 18:30:00 only. I tried do that with below code and it give me the desired result. However when it is sent to the server its like 5/31/2021 1:00:00 PM in C#. My local develop machine is in IST Zone.
export const dateToUTC = (date: Date): Date => {
let _date = new Date(
date.getUTCFullYear(),
date.getUTCMonth(),
date.getUTCDate(),
date.getUTCHours(),
date.getUTCMinutes(),
date.getUTCSeconds(),
date.getUTCMilliseconds()
);
return _date;
};
Can anyone please help me this query? I think the main issue is GMT+0530 which is being passed to server. I can do some tweaks and paly with the offset value but it might not work always.
For your case I would suggest using moment.js library.
The code would look like this:
// Moment wrapper object
var m = moment.parseZone('2021-06-01T00:00:00+05:30').utc();
// Default format
console.log(m.format()); // "2021-05-31T18:30:00Z"
// Your string for the server
console.log(m.format("ddd MMMM DD YYYY HH:mm:ss")); // Mon May 31 2021 18:30:00
JSFiddle
Indeed there is some tweaking underneath, but at least you can rely on it.

Moment date conversion from datepicker format problem

I have a problem with converting date time which comes from datepicker to moment (I use this library) time format.
What I get from datepicker: 2021-01-30T07:00:00.000Z
The code that I used via moment:
let tempTime = moment(dateString).toDate()
Output I get with this implementation: Fri Apr 30 2021 00:00:00 GMT-0700 (Mountain Standard Time)
What I expected: Fri Apr 30 2021 07:00:00 GMT-0700 (Mountain Standard Time)
The difference is between hours. In my implementation they just being ignored.
How can I overcome this issue?
Thanks for your attention!
The problem with conversion is the date string is in an UTC format. Either you remove the Z at the end or you can provide custom format and escape the UTC identifer like below.
const dateString = "2021-01-30T07:00:00.000Z";
let tempTime = moment(dateString, "YYYY-MM-DDTHH:mm:ss[Z]").toDate();
console.log(tempTime);

How to convert JSON time stamp in javascript to date and time?

I am getting Time stamp as "2020-03-02T05:50:31.000Z"
How to convert this to Normal Readable Format with Date and Time Zone
const parsedDate = new Date("2020-03-02T05:50:31.000Z");
console.log(parsedDate.toGMTString())
//"Mon, 02 Mar 2020 05:50:31 GMT"
console.log(parsedDate.toLocaleString())
//"3/2/2020, 11:20:31 AM"
console.log(parsedDate.toDateString(), parsedDate.toTimeString())
//Mon Mar 02 2020 11:20:31 GMT+0530 (India Standard Time)
In java-script above date format can be parsed by using Date.
Eg:
var date = new Date('2020-03-02T05:50:31.000Z');
I don't know what you mean readable format but you can use following methods:
new Date('2020-03-02T05:50:31.000Z').toLocaleString();
// outputs date according to user locale settings
Or you can use getYear, getMonth, getDay methods to get them and format date as you want
You can use moment.js for date time format and conversion.
Pass your date timestamp as below:
moment.parseZone("2020-03-02T05:50:31.000Z").format("DD-MM-YYYY hh:mm:ss z Z")
Modify format as you need ref mentioned here
Use in your code as mentioned here

Change timezone of date in Javascript but NOT the date or time

I have a problem here. I need to change the timezone of of a date in Javascript to UTC before passing it to the back end pf my service for validation. I cannot find a solution in any of the questions on this site or on other sites. The problem is that every method I have tried so far is also changing the time and date of my Javascript date object. For example:
var startDate = new Date($("#start-date-picker").val()).toUTCString();
Changes the time which affects the date (the timezone of my laptop is currently set to GMT +2).
While debugging my date object looks like this:
var startDate = new Date(getProperDate($("#start-date-picker").val()));
//startDate = Wed Aug 26 2015 00:00:00 GMT+0200 (Romance Daylight Time) {}
But when changed using the .toUTCString() method the date ends up like this:
startDate2 = "Tue, 25 Aug 2015 22:00:00 GMT"
I cannot find a way to change just the timezone and preserve the current date and time. I cannot use any external libraries either before anyone suggests moment.js or any others. Any help would be much appreciated, thanks!

Why does moment.js return this date wrong?

I'm simply trying to take an input string and convert it to a date object.
moment.utc('2000-01-01T00:00:00.000Z').toDate()
But it returns this...
Fri Dec 31 1999 19:00:00 GMT-0500 (EST)
Thanks
That is a valid JavaScript Date Object. You can test this by opening a console in Chrome or Firefox, then entering the following:
// Mon Nov 24 2014 09:54:00 GMT-0800 (PST) (looks the same as your example)
console.log( new Date() );
If you want to format the value coming out of moment.js, you can use its format method and a mask.
// Example: November 24th 2014, 09:58:12am
var fdate = moment().format('MMMM Do YYYY, h:mm:ss a');
Moment.js doesn't modify the prototype, it simply wraps it.
If you want to convert a string to a date object using moment.js, you can just call it as such:
moment(your_date); // Unless in UTC mode this will display as local time
In your instance you're using the UTC mode.
2000-01-01T00:00:00.000Z is the GMT date/time.
Using moment.utc("2000-01-01T00:00:00.000Z").toDate() returns this date/time according to your timzone settings.
See : http://www.digitoffee.com/programming/get-local-time-utc-using-moment-js/94/
Hope it helps.

Categories