how to convert UTC date/time into MDT and military time for react application? - reactjs

I am trying to convert UTC into MDT (mountain time) format. I am not sure which moment format require to convert date time into MDT. i tried moment.fromOADate(41493) but not working. TIA!

Mountain time is GMT -6, so you should be able to use moment().utcOffset(-6)
https://en.wikipedia.org/wiki/Mountain_Time_Zone
https://momentjscom.readthedocs.io/en/latest/moment/03-manipulating/09-utc-offset/

If you're worried about accounting for daylight savings time and can't just subtract 6 hours, you might also look into the moment.js timezone library.
You could say something like is described here and do var zone = moment.tz.zone('America/Chihuahua') (this is mountain time), then get your time offset with zone.parse(utcTimestamp)

Related

Convert time from UTC to Local timezone

I'm sending an email and I want to get the time when a file is uploaded, but the default time is in UTC.
I tried several things to convert from UTC to Central Time for example. Is there any expression to convert from UTC in Azure Logic Apps?
I tried the following expression:
convertTimeZone('HH:mm:ss', 'UTC', 'Pacific Standard Time')
As suggested by Timothy Radier , you can use combination of both formatDateTime ,convertTimeZone expression to covert the UTC time zone to Central standard time zone.
formatDateTime(convertTimeZone(utcNow(), 'UTC', 'Central Standard Time'),'HH:mm:ss')
Alternatively , By default central time zone is 5 hours behind the utc time you can also use 'subtractFromTime' expression to convert utc time to central time.
formatDateTime(subtractFromTime(utcNow(),5,'Hour'),'HH:mm:ss')
Here is the reference output :

timezone conversion using date-fns

I’m trying to work with date-fns-tz in my react-based webpage and couldn’t make the following use-case to work.
I have a date input in a form that should be submitted to the backend that stores the data in local timezone.
A user in GMT+2 timezone selects 14:00 on 1/Feb/2021 in the UI, which correlates to 1612180800 timestamp (as the UI was opened in GMT+2), but it should eventually get sent to the backend as 14:00 in GMT-8, which is actually 1612216800 timestamp.
What’s the right way to get this conversion (from 1612180800 --> 1612216800 ) to work?
I tried to work with various date-fns functions, but hadn’t found the right one.
You'll need two things to make this work correctly:
An IANA time zone identifier for the intended target time zone, such as 'America/Los_Angeles', rather than just an offset from UTC.
See "Time Zone != Offset" in the timezone tag wiki.
A library that supports providing input in a specific time zone.
Since you asked about date-fns, you should consider using the date-fns-tz add-on library.
Alternatively you could use Luxon for this.
In the past I might have recommended Moment with Moment-TimeZone, but you should review Moment's project status page before choosing this option.
Sticking with date-fns and date-fns-tz, the use case you gave is the very one described in the docs for the zonedTimeToUtc function, which I'll copy here:
Say a user is asked to input the date/time and time zone of an event. A date/time picker will typically return a Date instance with the chosen date, in the user's local time zone, and a select input might provide the actual IANA time zone name.
In order to work with this info effectively it is necessary to find the equivalent UTC time:
import { zonedTimeToUtc } from 'date-fns-tz'
const date = getDatePickerValue() // e.g. 2014-06-25 10:00:00 (picked in any time zone)
const timeZone = getTimeZoneValue() // e.g. America/Los_Angeles
const utcDate = zonedTimeToUtc(date, timeZone) // In June 10am in Los Angeles is 5pm UTC
postToServer(utcDate.toISOString(), timeZone) // post 2014-06-25T17:00:00.000Z, America/Los_Angeles
In your case, the only change is that at the very end instead of calling utcDate.toISOString() you'll call utcDate.getTime().
Note that you'll still want to divide by 1000 if you intend to pass timestamps in seconds rather than the milliseconds precision offered by the Date object.
You can use 'moment' to convert timezone.
1.Create a moment with your date time, specifying that this is expressed as utc, with moment.utc()
2.convert it to your timezone with moment.tz()
For example
moment.utc(t, 'YYYY-MM-DD HH:mm:ss')
.tz("America/Chicago")
.format('l');

Save Date on SQL Server with wrong timezone

I have this:
Clock = Date.now();
this.newReport.DateTime = new Date(this.Clock);
Client side the date is ok: Mon May 20 2019 19:08:34 GMT+0200
But on SQL Server it is save with time 17:08
Why?
Thanks
It looks like that's the same date, when you include the Timezone offset. If the "client side" date is GMT +0200, then the datetime stored should be 17:08:34 (as GMT time).
You're not including much detail, but I expect that you are wanting to save the time as local time. You could either convert the time to Localtime before saving it (and lose the additional information about the timezone), or save the GMT offset along with the date and time so that you could have both available if you wanted to convert it back to localtime later.

Find out client time zone in UTC format in Angularjs

Is there some way to find out what is the time zone of a user, in UTC format(like UTC+1, UTC+2,...etc)
What I am trying to accomplish is that after user selects UTC time zone from drop down and selects time and date, I want to show date and time values in his time zone (time zone from browser or system).
So if for example user selects: UTC+2 and 13:00 11-03-2016 and his system time zone is UTC than I want to show in some label: In your time that is: 11:00 11-03-2016 (since the UTC is minus 2 hours comparing to UTC+2)
Does somebody has suggestions on how to accomplish something like this?
Just create new Date object without specifying the time zone - JavaScript will use the browser's time zone and when getting a date, without specifying the time zone, the result is converted to the browser's time zone.
Please review examples on http://www.w3schools.com/js/js_dates.asp
Hope it will help. Handling dates in js and angular is a bit tricky;)

how to convert time of the day in UTC time?

i have time of the day in seconds and i have to find the utc time since 1970, is there any api which can directly convert this time to utc time,
i m using ansi c , and working on windows platform
platform independent api will be preferred
thanx in advance.
You can fill in a tm struct with the time of day you have (you'd have to convert it into seconds/minutes/hours) and the date. Then convert it with mktime() in time.h.

Resources