In the MySQL DB: '2020-04-19 22:00:00'(UTC). That's also what my endpoint returns since I set the connection option dataStrings:true.
On the client, after I fetch date:
const timezone = moment.tz.guess();
const convertedDate = moment(date)
.tz(timezone)
.format();
convertedDate then equals to "2020-04-19T22:00:00+02:00" (I'm in the UTC+2 zone).
I would like to get it in the format "2020-04-20T00:00:00" instead. How can I do that?
It looks like moment(date) believes your incoming date value is in local time, not UTC. So, your timezone conversion to local time changes nothing. You can tell moment it's UTC, like this:
const timezone = moment.tz.guess();
const convertedDate = moment.utc(date)
.tz(timezone)
.format();
You do not need moment-timezone for this. With Moment by itself you can use the utc function when parsing, and the local function to convert to the user's local time zone before formatting.
moment.utc('2020-04-19 22:00:00').local().format()
//=> "2020-04-20T00:00:00+02:00"
Also, the Moment team recommends using Moment for existing projects only. For new development, we recommend using Luxon instead:
luxon.DateTime.fromSQL('2020-04-19 22:00:00', {zone: 'utc'}).toLocal().toISO()
//=> "2020-04-20T00:00:00.000+02:00"
Related
I made a simple app displaying local time, utc time and the timezone offset. By default my qt app uses my local timezone "Europe/Amsterdam". But i want it to change when a new timezone is selected with the QCombobox which has a list of all the iana timezoneid's. But i can't find a method/function to change the default timezone to let say "Europe/Berlin" or any other timezoneid.
Eventually found the solution for my question. It seems i had to send a timezoneid with QDatime object:
QString comboxs = ui->comboBox->currentText();
QByteArray timezoneQstring = comboxs.toLocal8Bit();
timezoneids = timezoneQstring.data();
QDateTime timeobj = QDateTime(QDate(2019, 11, 5), QTime(20,28), QTimeZone(timezoneids));
I'm getting an error
converting data type nvarchar to datetime.
Where I did mistake?
var exec = db.Database.ExecuteSqlCommand("sp_InsertTicketChat #TicketId,
#FullName, #Description,
#LastCorrespondanceBy,#LastCorrespondanceOn",
new SqlParameter("#TicketId", TicketId),
new SqlParameter("#FullName", FullName),
new SqlParameter("#Description", Description),
new SqlParameter("#LastCorrespondanceBy", FullName),
new SqlParameter("#LastCorrespondanceOn",
DateTime.Now.ToString())
);
This is my stored procedure through which I want to insert data. What can I do ?
INSERT INTO tblTicketChat
(
TicketId,
FullName,
[Description],
LastCorrespondanceOn,
LastCorrespondanceBy
)
VALUES
(
#TicketId,
#FullName,
#Description,
GETDATE(),
#LastCorrespondanceBy)
Now I got the root cause of the issue you were facing.
Ideally, DateTime.Now.ToString() should have got converted to SQL Server Datetime data type when being assigned to the #LastCorrespondanceOn parameter of your stored procedure but it threw an exception.
The reason is date time format settings of your operating system.
The thing is when you perform DateTime.Now.ToString() in your C# language based client code it takes the default settings of date time format from your operating system. Look at the date time format currently set on my windows 10 box:
Due to this setting the code DateTime.Now.ToString() emits 27-07-2017 18:07:37. The output you're seeing is dd-MM-yyyy format.
Now this dd-MM-yyyy date format is not recognizable by SQL Server as per your default language settings.
SQL Server can recognize two date time formats when sent as string from client side
International date format yyyy-MM-dd also known as ISO 8601 .
Date format controlled by user login's dateformat setting. You can run dbcc useroptions command to see the value of setting. For me it is set to mdy
Due to this mismatch in the date time format being sent by your C# code (because of your operating system) and what SQL server can parse, you faced the issue.
When you did the conversion explicitly by date.ToString("yyyy-MM-dd"); you simply aligned it ISO 8601 international date time format supported by SQL Server so it started to work.
The default date time format (ISO 8601) of SQL Server is a configuration of the collation you are using currently which you can check in your SQL Server instance properties as shown below:
It is strongly recommended that you should always use SQL's date time format when passing it in string format as mentioned in this thread.
So you can solve your problem in three ways:
Change the default date format of your OS to align it to SQL Server (Not recommended). It will never be feasible on all client computers where your application runs.
Use custom date format while calling ToString method as you have done to align it to one of the formats supported by SQL Server. Best format is ISO 8601 format which you've used.
Don't convert it to string. Just pass the date time value as is.
var exec = db.Database.ExecuteSqlCommand("sp_InsertTicketChat #TicketId,
#FullName, #Description,
#LastCorrespondanceBy,#LastCorrespondanceOn",
new SqlParameter("#TicketId", TicketId),
new SqlParameter("#FullName", FullName),
new SqlParameter("#Description", Description),
new SqlParameter("#LastCorrespondanceBy", FullName),
new SqlParameter("#LastCorrespondanceOn",
DateTime.Now) //Don't call .ToString here
);
Approach # 3 is the best deal. More details here as to why you shouldn't use strings but the date time data type while dealing with SQL Server datetime columns.
I solve this issues by using MSSQL string formatted (dd-MM-yyyy) in my storedprocedure
I solved this issue by doing some changes.
DateTime date = DateTime.Now;
string strDateTime = date.ToString("yyyy-MM-dd");
var exec = db.Database.ExecuteSqlCommand("sp_InsertTicketChat #TicketId, #FullName, #Description, #LastCorrespondanceOn, #LastCorrespondanceBy",
new SqlParameter("#TicketId", TicketId),
new SqlParameter("#FullName", FullName),
new SqlParameter("#Description", Description),
new SqlParameter("#LastCorrespondanceBy", "raza"),
new SqlParameter("#LastCorrespondanceOn", strDateTime)
);
I have been trying to setup local notifications with Titanium mobile for iOS, using a custom date time, but its not working with custom date time. What I am trying is
var notification = Ti.App.iOS.scheduleLocalNotification({
alertBody:"Dummy text",
alertAction:"Re-Launch!",
userInfo:{"hello":"world"},
date: new Date("2015-03-22 01:45")
});
However when I use this for date, It works.
date: new Date(new Date.getTime()+8000)//Current date time + 8 secs after.
What should I do to make this work.
date: new Date("2015-03-22 01:45")
Thanks.
The date string you are passing to the constructor is not valid.
You could use something like:
date: new Date("1/20/2015 00:00:00")
Where the format is MM/DD/YYYY HH:MM:SS
I have a datestring in this format
yyyy-mm-ddThh:mm:ss[Z]
And i have a timezone string. for e.g "Asia/Kolkata"
Now i want to convert this date string into the timezone of the given timezone
for e.g. if the date is 2014-01-03T23:30:00Z , then in "Asia/Kolkata" timezone it will be 2014-01-04T05:00:00 .
I tried using Time library , but Time library does not seem to have any method which can convert to other timzone http://ruby-doc.org/core-1.8.6/Time.html#method-c-mktime .
You should use the TZInfo gem.
require 'tzinfo'
tz = TZInfo::Timezone.get('Asia/Kolkata')
utc = DateTime.iso8601('2014-01-03T23:30:00Z')
local = tz.utc_to_local(utc)
If it just a ruby project without rails, also you do not want to install third party gems.
Then you have to do it yourself, the following is what I did:
def convert_time_to_timezone(time, timezone)
timezone_in_hours = timezone.to_i
time_in_seconds = time.to_i
time_in_seconds_in_timezone = time_in_seconds + timezone_in_hours*3600
utc_time = Time.at(time_in_seconds_in_timezone).utc
Time.new(utc_time.year, utc_time.month, utc_time.day, utc_time.hour, utc_time.min, utc_time.sec, "#{timezone}:00")
end
Just provide time (e.g., Time.now) and timezone (e.g., "+11:00", or "-05:00")
it will return the time with the specified timezone.
example:
call convert_time_to_timezone(Time.now, "+11:00") it will return something like
2017-05-31 18:17:13 +1100
If it has rails installed, then you can directly call in_time_zone('"Central Time (US & Canada)"')
I am trying to get the timezone from native calendar using the following code but i am getting the timezone has Asia/Calcutta instead of just 'IST'
Calendar calendarLocal = Calendar.getInstance();
// fetches time zone
TimeZone timeZone = calendarLocal.getTimeZone();
System.out.println("Time Zone getAvailableIDs() --->"+timeZone.getAvailableIDs());
String[] x=timeZone.getAvailableIDs();
for(int i=0;i<x.length;i++){
System.out.println("Time Zone IDs-->"+x[i]);
}
System.out.println("Time Zone ID--->"+timeZone.getID());
System.out.println(" Calender Default-------->>>"+timeZone.getDefault());
System.out.println("Time Zone --->"+timeZone.getTimeZone(timeZone.getID()));
Here TimeZone is Asia/Calcutta i need it to print IST
BlackBerry Java doesn't give you the time zone short codes, at least not reliably (it only gaurantees to know about the "GMT" code). You can see my answer here for information about how to code a mapping between strings like "Asia/Calcutta" and "IST". (my method mapTimeZoneCodes() in that example)
I provide a template method for setting up the mapping, and a link to this article on Desktop Java, which seems to have a pretty complete list of the time zone codes, and how to map codes to the long Java time zone strings.
It will be boring work to copy the strings into my template, but once you have it, you'll be able to easily lookup the short code based on the long name:
String longName = timeZone.toString();
String shortCode = (String)_timeZones.get(longName);