I've got an input date as the following:
Thu May 17 2012 18:00:00 GMT+0100 (BST)
However, with the following :
var dateString = 'hu May 17 2012 18:00:00 GMT+0100 (BST)';
document.write($.format.date(dateString, "ddd MMMM dd, HH:mm"));
The resulting output is 1 hour faster than I'd expect:
Thursday May 17, 18:00
Seems to be ignoring the GMT+0100 (BST) part...
Is there anyway to get it to display as 17:00?
You will have to convert the timezone explicitly. You can create a separate function for it.
Please check this link :
http://www.techrepublic.com/article/convert-the-local-time-to-another-time-zone-with-this-javascript/6016329
Related
I've been struggling for days with some DateTime values.
I have an API backend that uses entity framework and sql server with .netcore.
The big issue when i want to send a datetime from angular to c#
backend. I noticed that Date() in typescript/javascript by default
uses my timezone and i don't know how to exclude it.
For example my date looks like this:
Wed Jul 11 2019 21:00:00 GMT+0300
And when it arrived in c# it becomes 07/10/2010(mm-dd-yyyy), it subtracts 1 day due to timezone.
Is there a way to standardize the Date variable to ignore timezone and always keep the same format DD-MM-YYYY ?
I've also tried to use MomentJS and still can't figure it out, even my MomentJS compares are acting strange due tot his issue.
For example:
const VacationStart = moment(calendarEntity.Vacation.StartTime).utc(false);
const VacationEnd = moment(calendarEntity.Vacation.EndTime).utc(false);
if (VacationStart.isSameOrBefore(ColumnDate,'day') && VacationEnd.isSameOrAfter(ColumnDate,'day')) {
return '#FF0000';
}
In the above example:
VacationStart is Wed Jul 10 2019 21:00:00 GMT+0300
VacationEnd is Wed Jul 17 2019 00:00:00 GMT+0300
ColumnDate is Thu Aug 15 2019 03:00:00 GMT+0300 (incremental value)
Yet for some reason even if i use isSameOrBefore(ColumnDate,'day') to specify to compare only up to days it still does not work. When VacationEnd should be equal to ColumnDate is return false.
Note: everything is in a foreach loop where ColumnDate increases by +1 day.
You just need to use UTC time (Greenwich Mean Time)
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/UTC
https://learn.microsoft.com/en-us/dotnet/api/system.datetime.utcnow?view=netcore-2.2
So something like this:
new Date(new Date().toUTCString()); -- "Mon Jul 01 2019 17:55:41 GMT-0700 (Pacific Daylight Time)"
new Date().toUTCString(); -- "Tue, 02 Jul 2019 00:56:38 GMT"
new Date().toString(); -- "Mon Jul 01 2019 17:57:03 GMT-0700 (Pacific Daylight Time)"
I am using the date input type so users can select dates:
<input type="date" name="start" ng-model="formData.start">
Before the data is sent to the server, I see it as this:
console.log("start: " + $scope.formData.start);
start: Sat Jan 09 2016 00:00:00 GMT +0100 (CET)
In my MySQL database it is saved as this:
2016-01-08 23:00:00
So it seems like there is a difference of 1 hour. The problem is that this hour changes the date from one day to the day before. Is there a way to change this?
The date is correct:
You input Sat Jan 09 2016 00:00:00 GMT +0100 (CET) note the GMT +0100. That indicates that the timezone of this date is one hour off (later).
So without the timezone 2016-01-08 23:00:00 is perfectly correct! (GMT +0)
TLDR: both dates are the same
Example:
var date = new Date("Sat Jan 09 2016 00:00:00 GMT +0100 (CET)")
document.write(date.toUTCString())
moment('Sat Sep 12 2015 15:00:00 GMT+0100 (GMT Daylight Time)').format()
// Error: core-test.js:52920 Uncaught Error: input not handled by moment(…)
I am miffed as to why moment cannot handle this date, even when stripping it down I still can't get it to work it out.
var date = 'Sat Sep 12 2015 15:00:00 GMT+0100 (GMT Daylight Time)'.split(' GMT')[0];
moment(date).format()
//The same error
You can provide moment a format to use to parse your string: http://momentjs.com/docs/#/parsing/string-format/
But your date format isn't possible with the options available so you have to strip out some of the information. Namely the reference the timezone name for the offset "GMT" and the "GMT Daylight Time". Since the timezone is encoded in the "+0100" part I'm going to assume that it's fine to remove those references.
First create a function to "clean" the string date:
function cleanDateString(formattedDate) {
return formattedDate.replace(/(.*?)(\w{3})(((\+|-)\d{4}).*)/g,"$1$4");
}
If you invoke the function on the string you provided cleanDateString('Sat Sep 12 2015 15:00:00 GMT+0100 (GMT Daylight Time)') the output will be
"Sat Sep 12 2015 15:00:00 +0100"
Now this is something that can be parsed by moment using the right format.
If you look at http://momentjs.com/docs/#/displaying/format/ then the format that you would need for this string is
'ddd MMM DD YYYY mm:hh:ss ZZ'
If you combine the two things from above, then you can get your date like this
function parseCustomDate(formattedDate) {
return moment(cleanDateString(formattedDate),'ddd MMM DD YYYY mm:hh:ss ZZ');
}
And that will work with your given string
parseCustomDate('Sat Sep 12 2015 15:00:00 GMT+0100 (GMT Daylight Time)')
Note on the Regex
If you want details on how exactly the regex works, you can look at the "Explanation" and the "Match Information" sections at this link: https://regex101.com/r/tH6hM9/1. I used that to tweak the groupings and the rules
Your Date Format should look like this:
var date2 = '2015-09-12T15:00:00+00:00'; // UTC
There is the following code:
new Date("2000-01-01T18:00:00.000Z")
I got:
Date {Sat Jan 01 2000 21:00:00 GMT+0300 (MSK)}
But I need to get:
Date {Sat Jan 01 2000 18:00:00}
i.e. time without specific timezone. How can I do it? Thanks!
I'm loading some dates comming from my database into a HTML table in a string format. The string looks like 31-AUG-13 I'm parsing this string into a date object using the below code:
var paymentDate = $(this).find('td.paymentDate').text();
var test = $.datepicker.parseDate('d-M-y', paymentDate);
Everything is ok so far and I'm getting this date object: Date {Sat Aug 31 2013 00:00:00 GMT+0300 (FLE Standard Time)} But once the year is bigger than 2023. In my case 31-JAN-24 and so on it is turning to 1924 and not 2024, so I'm getting these date objects:
Date {Thu Jan 31 1924 00:00:00 GMT+0200 (FLE Daylight Time)}
Date {Fri Feb 29 1924 00:00:00 GMT+0200 (FLE Daylight Time)}
Date {Mon Mar 31 1924 00:00:00 GMT+0300 (FLE Standard Time)}
Date {Wed Apr 30 1924 00:00:00 GMT+0300 (FLE Standard Time)}
And so on. My question is regarding this strange issue. Is there a way to declare the year range and why it is going back to 1900 in the case when the year is bigger than 2023?
This teaches a lesson always use year in full format same was case with y2k problem. Convert date from database in to yyyy format then use it.