I want to calculate the difference of time in hours between 2 different time zones using the moment.js library.
I tried using diff function provided by the library but it is not providing correct answers, I tried:
moment('2016-12-18 6:00:00', 'YYYY-MM-DD HH:mm:ss')
.diff(moment('2016-12-18 6:00:00', 'YYYY-MM-DD HH:mm:ss').utcOffset('+09:30'), 'hours')
I am in UTC+5:30 and the moment(string, string) constructor returns the time in my current timezone. And I was expecting the answer to be 4 instead got 0. Is there any other function that moment library provides to get difference amongst timezones ?
As long as you are just using fixed time zone offsets, your above code will work if you make one adjustment:
.utcOffset('+09:30', true)
The second parameter tells the function to retain the date and time value the moment already has when the offset is set. The default (false) will shift the date and time from its current offset (your local time) to the offset provided.
If you are not working with fixed offsets, then you will indeed need to use the moment-timezone add-on.
Related
I'm trying to convert java datetime instant to hh:mm format using moment js
moment("2020-03-21T17:34:00Z").utcOffset(-0500).format("hh:mm")
it should give me 12:34, but somehow it is giving "12:14" which is the wrong time.
The moment js .utcOffset() method takes the offset in minutes.
so if you want to get 12:34 you need to use -300 instead of -0500
moment("2020-03-21T17:34:00Z").utcOffset(-300).format("hh:mm")
A few things:
The utcOffset function behaves differently whether you pass a string or a number. As a number, it's expected to be in terms of minutes. Since you have hours and minutes in your offset, you should pass it as a string: .utcOffset("-0500")
Format strings are case sensitive. You should use HH:mm (24-hour time), or hh:mm a (12-hour time). Since you used hh:mm without an a, anything after 12 pm will be misrepresented.
You are supplying a fixed offset. If that is your intent, then ok. But do recognize that there is a difference between an offset and a time zone. (See "Time Zone != Offset" in the timezone tag wiki.) For example, if you wanted to convert to US Eastern Time, you should use .tz('America/New_York) instead of .utcOffset("-0500"). (US Eastern time is at -4 for the date given, not -5.) You would need the moment-timezone addon to make this work.
Lastly, recognize that Moment is in maintenance mode. The moment team generally recommends Luxon for new development.
I'm using momentjs to convert String to date in with full value of date and time and the result always increases about 12 hours. just like the picture below.
So how come does it go like this and how I can do to solve the problem. Many thanks.
"Z" at the end indicates that the date is provided with UTC-0 zone. momentjs internally shifts the timezone to suit your own. You should omit it if you want to reformat provided datetime string.
How can I create a user defined variable that has a relative date e.g. current date plus 1 year?
I have created a user define variable startDate
I have tried adding the code
LocalDate.now().plusYears(1).toString();
in multiple paces but I just can't get the variable value to be set by the code.
Use __timeShift function:
${__timeShift(dd/MM/yyyy,${startDate},P1D,,)}
The timeShift function returns a date in the given format with the specified amount of seconds, minutes, hours, days or months added
You need to include the java.time package into your expression
The correct syntax for the __groovy() function would be:
${__groovy(java.time.LocalDate.now().plusYears(1).toString(),startDate)}
Demo:
More information: Creating and Testing Dates in JMeter - Learn How
I am calling a service which standardizes a given address and also gives timezone of the result in UTC offset (ex: -5:00 etc).
Is there a function in peoplecode which takes in timezoneoffset of UTC and calculate timezone ?
Have you tried this:
DateTimeToTimeZone
DateTimeToTimeZone
Syntax
DateTimeToTimeZone(OldDateTime, SourceTimeZone, DestinationTimeZone)
Description
Use the DateTimeToTimeZone function to convert DateTime values from the DateTime specified by SourceTimeZone to the DateTime specified by DestinationTimeZone.
Considerations Using this Function
Typically, this function is used in PeopleCode, not for displaying time. If you take a DateTime value, convert it from base time to client time, then try to display this time, depending on the user settings, when the time is displayed the system might try to do a second conversion on an already converted DateTime. This function could be used as follows: suppose a user wanted to check to make sure a time was in a range of times on a certain day, in a certain timezone. If the times were between 12 AM and 12 PM in EST, these resolve to 9 PM and 9 AM PST, respectively. The start value is after the end value, which makes it difficult to make a comparison. This function could be used to do the conversion for the comparison, in temporary fields, and not displayed at all.
Example
The following example. TESTDTTM, is a DateTime field with a value 01/01/99 10:00:00. This example converts TESTDTTM from Pacific standard time (PST) to eastern standard time (EST).
&NEWDATETIME = DateTimeToTimeZone(TESTDTTM, "PST", "EST");
&NEWDATETIME will have the value 01/01/99 13:00:00 because EST is three hours ahead of PST on 01/01/99, so three hours are added to the DateTime value.
I need to set a chart to start at midnight of the current day and end just before midnight of the next day...
I'm trying to do something like this:
minChartDate = currentDate.fullYear,currentDate.month,currentDate.date,0,0,0,0;
where currentDate:Date; is the day currently selected.
I'm getting an implicit coercion error between the Number to Date type, as if currentDate.fullYear is a Date, but according to the documentation it should be a number. Or is my syntax defining this incorrect? Also wondering if there's a simpler way to get the min and max dates than this!
(the reason I am setting this is so that it starts at midnight rather than at the first data point in the series).
I'm also getting a weird error 'maximum' values of type Date cannot be represented in text.. it said I need a Date type object for the minimum and maximum so I'm really not sure what it's talking about...
this code will make a date object set to 0:00 on today.
var minChartdate:Date= new Date();
minChartdate.hours=0;
minChartdate.minutes=0;
minChartdate.seconds=0;
minChartdate.milliseconds=0;
trace(minChartdate)
To make one for the next day:
var minChartdate:Date= new Date();
minChartdate.time = minChartdate.time+1000*60*60*24 // one day in milliseconds
minChartdate.hours=0;
minChartdate.minutes=0;
minChartdate.seconds=0;
minChartdate.milliseconds=0;
trace(minChartdate);
This script moves the date object forward by 24 hours, and then sets hours, mins,sec,milisecs to 0.
Note: This is not 100% correct solution, it can fail on days, when the hours are adjusted because of daylight saving time changes.