I'm having a bit of trouble with the mongodb c# driver, in that it seems to be converting all my dates to a UTC form.
I have
Dim cDate as Date
Dim year as integer = 2012
Dim month as integer = 12
Dim day as integer = 21
cDate = New Date(year, month, day)
However putting it into a mongodb database via the C# driver seems to convert it to UTC so all of a sudden its a different day now because its now 11:00 PM 20th December 2012. Not exactly what I wanted!
Is there a way I can create the New Date(year, month, date such that its an UTC mode to begin with? So if i did cDate.utcNow I would get the same thing as cDate, in essence cDate.utcNow = cDate?
I have tried all sorts of stuff with the driver only to run into a brick wall such as using the DateTimeSerializationOptions.Defaults to no avail nothing happens!
To create your date as a UTC date, just specify the Kind parameter in the constructor along with the hours, minutes, and seconds:
cDate = New Date(year, month, day, 0, 0, 0, DateTimeKind.Utc)
Related
I have this string: 28 June 2018 (22:05)
How can I compare it with my current time and get the difference?
For example if actual time was 29/06/2018 (05:49)
The difference will be: 7 hours 44 minutes
So input: 28 June 2018 (22:05)
Output: 7 hours 44 minutes
The first thing you need to do, is convert the string to a valid DateTime instance.
If you know your dates will always be in this format, you can do the following...
Dim mydate = DateTime.ParseExact("28 June 2018 (22:05)", "dd MMMM yyyy (HH:mm)", CultureInfo.InvariantCulture)
https://msdn.microsoft.com/en-us/library/w2sa9yss(v=vs.110).aspx
Once you've parsed the string into a valid DateTime instance, you can use all the normal date functions to do the comparisons.
I would first get the difference in minutes, like so...
Dim diffminutes = DateDiff(DateInterval.Minute, mydate, Now)
Then create a timespan like this...
Dim mytimespan = TimeSpan.FromMinutes(diffminutes)
Finally display the difference in hours and minutes like this...
Response.Write(mytimespan.ToString("hh\:mm"))
We have timestamp stored in utc in format "2017-08-25 14:34:20.0000000 +00:00". Our customer selects timestamps directly from db and they want to add additional hours in order to fit timestamp stored with their timezone. Can I set a select query to make it to add hours to current timestamp? Database sql-server
Maybe this would help:
The DATEADD() function adds or subtracts a specified time interval from a date.
DATEADD(interval, number, date)
interval Required. The time/date part to return. Can be one of the
following values:
year, yyyy, yy = Year quarter,
qq, q = Quarter month,
mm, m = month
dayofyear = Day of the year day, dy,
y = Day
week, ww, wk = Week
weekday, dw, w = Weekday
hour, hh = hour
minute, mi, n = Minute
second, ss, s = Second
millisecond, ms = Millisecond
number Required. The number of intervals to use
date Required. The date to which the interval should be added
DATEADD(HOUR,1,fieldFromSQL)
I find the new command "AT TIME ZONE" very useful for this.
This requires SQL Server 2016 sp1.
Here is how:
DECLARE #FieldFromSQL DATETIME2= GETUTCDATE()
SELECT #FieldFromSQL AS DateTimeInUTC , #FieldFromSQL
AT TIME ZONE 'utc'
AT TIME ZONE 'Central Europe Standard Time' AS DateTimeInLocalTime
It also works across Daylight time changes, with which I've had many problems.
I have a user input where the user enters year, month and day and I create a date object
let userDate = new Date(year, month, day)
and then from a user input where the user enters minutes and hours (into variables minutes and hours)
I need to convert this date and time into UTC timestamp, using moment and I'm not sure about it. Can I just do
let utcTimestamp = moment.utc(date.toISOString()).hour(hours).minutes(minutes);
For example: if a user enters a date of 13-Mar-2018 and time of 8:45 AM (in GMT timezone), then I could use the above line of code to get UTC timestamp as I can directly add hours and minutes to the date
if a user enters a date of 13-Aug-2018 and time 8:45 (which is GMT +1, due to daylight savings time change) then with above line I might be creating a wrong date.
... I create a date object
let userDate = new Date(year, month, day)
Be careful here, you need to subtract 1 from the month, as they are numbered 0-11 by the Date instead of the usual 1-12.
and then from a user input where the user enters minutes and hours (into variables minutes and hours)
I need to convert this date and time into UTC timestamp, using moment ...
You don't need Moment for this. Just call .toISOString() on the Date object. It implicitly converts to UTC before generating the string.
var utcString = new Date(year, month-1, day, hours, minutes).toISOString();
... Can I just do
let utcTimestamp = moment.utc(date.toISOString()).hour(hours).minutes(minutes);
No, that doesn't make sense. That would only work if the date was entered in local time but the time was entered in UTC. Such a difference in behavior would surely be confusing to your user - especially if the UTC date was different than the local date.
... if a user enters a date of 13-Aug-2018 and time 8:45 (which is GMT +1, due to daylight savings time change) then with above line I might be creating a wrong date.
The conversion from local time to UTC will automatically take into account any daylight saving adjustment that is being observed by the local time zone. You do not need to do any additional adjustment on your own.
This is a snippet i used to convert an outlook calendar event from UTC to local time. The same Technique could be used for other scenarios.
//Get the users timezone
let timeZone = item.originalStartTimeZone;
//Get the start datetime in UTC time
let timeStart = item.start.dateTime;
//Calculate the negative offset.
let offset = - timeStart.localeCompare(timeZone);
//Add the calculated offset to the UTC start time
let localStartTime = addHours(timeStart, offset);
Dim DespatchDate As DateTime
Dim ReceiptDate As DateTime
DespatchDate = #3/7/2017 12:00:00 AM# 'I am in the UK so this is 3rd July 2017
ReceiptDate = DespatchDate.AddDays(60) 'Returns #5/6/2017 12:00:00 AM#
I would expect "1/9/2017" the 1st September 2017
I also tried
ReceiptDate = DateAdd("d", 10, DespatchDate) which returned #3/17/2017 12:00:00 AM#
I must be doing something wrong but what?
The date literal in VB uses the US format regardless of where you are. Your code:
DespatchDate = #3/7/2017 12:00:00 AM# 'I am in the UK so this is 3rd July 2017
is creating the date March 7 2017. Read more on date literals or use the constructor on the date class that makes it more apparent what you are doing.
Update
In order to be sure what the date is and to avoid ambiguity, you can define it this way:
DespatchDate = New Date(2017, 7, 3) ' Without the time of day, 12AM is implied
DespatchDate = New Date(2017, 7, 3, 12, 34, 56) ' July 3 2017 12:34:56 PM
The good thing with this is you can see from intellisense while typing the code what each number means. And if someone else had written the code, you can invoke intellisense to see what each argument means. As you can see from what I highlighted in my screenshot, the time is in 24 hour format.
I want to get timestamp of a different country in VBA. Is there any direct function or way to get it? For example, I am in India working for Mexico and I want to do certain task based on Mexican Time. I was able to get it by splitting the timestamp and manipulating it but could not consider daylight saving in it. Is there any simple solution than writing a big user-defined function?
Know your time zone offset & your client's time zone offset, then use that to calculate the difference
Dim IndiaTZ as single
Dim MexicoTZ as single
Dim MyTime as date
Dim UTC as date
Dim MexicoTime as date
IndiaTZ = 5.5
MexicoTZ = -4
'note this assumes that they're in Eastern time,
'Mexico also covers Central, Mountain & Pacific at -5, -6 & -7.
'You'll need to figure out which one you need.
MyTime = Now
'need to invert the offset to get from India to UTC
UTC = datediff("h", mytime, IndiaTZ * -1)
'need to invert the offset to get from UTC to Mexico
MexicoTime = Datediff("h", UTC, MexicoTZ * -1)