How to update timestamp with timezone field in postgresql using c# - c#

How to update a postgresql's timestamp with timezone data field with the C# code?
I expect datetime with timezone using codes below:
DateTime _now = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Unspecified);
TimeZoneInfo _timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById(TimeZone.Time_Zone_Id);
DateTime _new = TimeZoneInfo.ConvertTime(_now, _timeZoneInfo, _timeZoneInfo);
I expect: 2019-02-28 09:24:21.4367+00 before I update a table with this value.
Currently, I get the following error:
of type time with time zone but expression is of type timestamp without time
zone

Related

Convert from UTC C# SQL

Using a combination of server-side C#, SQL database, and the dayjs client-side library dayjs I am unable to convert the date stored as UTC in the database UTC to local time.
My theory is to store all date-times in the database as UTC. Use the client (browser) to determine the user timezone (automatically or store it for each user), then let the browser, via dayjs convert from UTC time to local time.
This is NOT a dayjs problem but the way dates are stored in SQL and pulled via C#.
In order for dayjs to convert the DateTime on the client app the date must be returned from the server in a very specific format.
If you are using DateTime C# and SQL datatypes and using just pulling the DateTime straight from the database to a DTO the date will be returned to the client like this:
noteDate: "2022-01-06T22:31:34.81"
In order for dayjs.tz() method to work the date DateTime must be returned from the server in this format:
noteDate: "2022-01-06T22:31:34.81Z"
Then this code will property convert from UTC to local time:
let date = "2022-01-06T20:14:18.917Z";
let timeZoneGuess = dayjs.tz.guess();
date = dayjs(date).tz(timeZoneGuess);
To get this format to the client I had to add additional information to the DateTime before returning the note.NoteDate to the client.
note.NoteDate = DateTime.SpecifyKind(note.NoteDate, DateTimeKind.Utc);
When storing the DateTime in SQL I am using c# DateTime and SQL DateTime datatypes. When setting the note.NoteDate I call note.NoteDate = DateTime.UtcNow but apparently SQL does not save the "Z" part of the UTC date.
I would like to see if anyone else has this same issue and if there are better ways to solve it.

Convert Datetime to date field to pass into Google BigQuery

I am uploading data into BigQuery Cloud by my c# Application. I have column in BigQuery table 'ForDate' with datatype 'Date'.
But in c# datatype is 'datetime'(As date datatype not supported in c#)
I am getting Below error on uploading data to bigquery:
{Invalid date: '2017-01-02T00:00:00' Field: ForDate; Value: 2017-01-02T00:00:00}
What could be the workaround to upload date with date part only and ignore time part from it?
This is working for me:
string currentdatetime = DateTime.Now.ToUniversalTime().ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fff'Z'");
From:
How can I format DateTime to web UTC format?
Import said column as a string, parse in BigQuery to a timestamp value later.
SELECT TIMESTAMP('2017-01-02T00:00:00')
The date field in the BigQuery schema should be marked as a TimeStamp not as a DateTime field. Then you can simply insert the row with:
bqr.Add(attribute, date.ToUniversalTime());
I know its been a while since this question was asked, but for me none of the answers was correct. It seems that the BigQuery NuGet doesn't like date times with specified kind (e.g UTC), and ToUniversalTime() produces an UTC kind.
The only trick that worked for me was to create a new instance of DateTime with none (Unspecified) kind.
var now = DateTime.UtcNow;
var dt = new DateTime(now.Year, now.Month, now.Day, now.Hour, now.Minute, now.Second, now.Millisecond);
You can use:
BigQueryDateTimeExtensions.AsBigQueryDate(DateTime.UtcNow)
From: Class BigQueryDateTimeExtensions
For DateTime use:
DateTime.UtcNow.ToString("s")

getting datetime from different location

I need a time from different location. My server resides in the US but I need time from Australia.
when I save records in database, I use DateTime.Now() C# function. Is there a way I can get and save Australian time even though the server where my website is hosted is in US?
If there is any database solution then that will be ok too. I am using sql server
This is how you get your local time now:
DateTime yourLocalTime = DateTime.Now;
You can get current UTC time with DateTime.UtcNow or by converting local time to UTC time:
DateTime utcTime = DateTime.UtcNow;
DateTime utcTime = yourLocalTime.ToUniversalTime();
Save this time to database instead of saving some local time. How to get your local time from UTC time:
DateTime yourLocalTime = utcTime.ToLocalTime();
How to get time in other time zone:
TimeZoneInfo timeZone = TimeZoneInfo.FindSystemTimeZoneById("Tokyo Standard Time");
DateTime tokyoLocalTime = TimeZoneInfo.ConvertTimeFromUtc(utcTime, timeZone);

DateTimeFormat issue when pass the parameter to MS-Sql stored procedure

I have a stored procedure, which has a parameter like #CurrentDate datetime, when I pass the value DateTime.Now from the front end (C#) to this stored procedure it is working fine.
But when I change the Date Format as English(India) in my system/server, that time DateTime.Now will return a value like 25-10-2012 PM 05:23:27. I am passing this value to stored procedure and I'm getting an error message like the following,
Msg 242, Level 16, State 3, Line 10
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
Note : I don't want to use GETDATE() from Sqlserver, I want to pass the parameter from c#.
How can I solve this?
The problem with the date format is because you are converting it to a string and then back to a DateTime value. Depending on the culture settings on the specific server this may or may not work. It may also misinterpret the data, e.g. transforming a date from 2013-10-12 to 2013-12-10.
DateTime datet = new DateTime(year,month,day);
startDateParam.Value = datet;
endDateParam.Value = datet;
"note that the server stored datetime with format '1900-01-01 00:00:00.000'"
You can use this conversion from C#. Hope this helps
DateTime.Now.ToString(DateTimeFormatInfo.InvariantInfo)
You will need to convert the value to a valid DateTime using Convert function.
For more information : http://www.sql-server-helper.com/tips/date-formats.aspx
Storing dates in local timezones can cause headache when you launch internationally.
If you save current date using GETUTCDATE() on SQL Server side, you can get the time in user specific timezone as follows:
public DateTime GetDateByTimeZoneId(DateTime dateTime, string timeZone)
{
if (dateTime == null)
{
return null;
}
dateTime = DateTime.SpecifyKind(dateTime, DateTimeKind.Utc);
return TimeZoneInfo.ConvertTimeFromUtc(dateTime, TimeZoneInfo.FindSystemTimeZoneById(timeZone));
}
You can then pass user relevant timezone like "GMT Standard Time"

Read a DateTime object in a different time zone

I have following scenario:
USA: database & application server
Europe: client
The server reads a date time (e.g. 12:00) object from the database and send it to a client in Europe. The problem is now, the client displays this date time in the time zone of the client (e.g. 18:00), but we need the time in the database, independent of the time zone of the server. On the client we don't know from which time zone this value is.
So how can we achieve this?
your tags tell the answer.
use the TimeZone Class.
http://msdn.microsoft.com/en-us/library/system.timezone.touniversaltime.aspx
also: Creating a DateTime in a specific Time Zone in c# fx 3.5
So in your DB, times should be UTC. from there you can do anything what you want.
Can't you simply use DateTime.ToUniversalTime()?
http://msdn.microsoft.com/en-us/library/system.datetime.touniversaltime.aspx
Alternatively, if you don't want UTC, you can find out the timezone of your server and do something like:
DateTime dt;
TimeZoneInfo timezone_EST =
TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
DateTime dt_EST = TimeZoneInfo.ConvertTime(dt, timezone_EST);
If you're storing the DateTime data in SQL 2008, take a look at new datetimeoffset type which will store timezone information as well as the date and time themselves

Categories

Resources