Convert Javascript date to shorter date time and also UTC [duplicate] - javascript

This question already has answers here:
Why does Date.parse give incorrect results?
(11 answers)
Where can I find documentation on formatting a date in JavaScript?
(39 answers)
Closed 5 years ago.
I have a JavaScript Date which I want to get it into SQL Server datetime field.
The JavaScript time sent over as
Fri Sep 15 2017 00:11:44 GMT-0700 (US Mountain Standard Time)
I want it to be converted in javascript or in C#
I need the date to be like
2017-09-15 23:47:01
OR perhaps
9/15/2017 11:47:01 PM
I need to also convert it to UTC , not sure if that is easier to do in javascript or in C#
Which type of format is my code even in? "Fri Sep 15 .." ??
For UTC I was seeing code like this
var isoDate = new Date('yourdatehere').toISOString();
( I am doing .net core web api, with Angular 4 / Typescript)

As Keith said, working with dates is really hard in javascript, especially considering very different implementations across the browsers. So I will second their opinion: just use moment.js and leave everything to that library. It is really powerful. Using it you can get any date format you can possibly think of. To get "2017-09-15 23:47:01" all you need is the following (please pay attention that qualifiers are case sensitive!):
var fmt = moment(<your_js_date_if_you_have_it>).format("YYYY-MM-DD HH:mm:ss");
You can find all supported qualifiers here.
Apart from formatting this library also allows you to do a lot of manipulations with the dates, compare them, translate dates from local to UTC time zone and back, etc.

Related

Javascript Date unnecessarily adjusts for timezone [duplicate]

This question already has answers here:
Parsing a string to a date in JavaScript
(35 answers)
Closed 2 years ago.
I am storing a date as a django DateField in my database. It simply looks like "2020-06-25" in the database.
This string is returned from my api, and passed into a javascript "Date" as such:
date = new Date(due_date)
logging this date results in:
Wed Jun 24 2020 17:00:00 GMT-0700 (Pacific Daylight Time)
I don't care about the time, only the date. How do I get javascript Date to ignore the time, and not adjust for the difference in timezone between the DB and the local user? If a user sets the due date for a project in one timezone, I want every person to see the same due date.
Converting a string to a date in JavaScript
This guided me to the answer. Browsers handle this differently, but you can force the date to display in UTC time, rather than letting the browser decide if it wants to covert to the end user's local time or not.

How to Convert datetime from one timezone to another timezone in Javascript? [duplicate]

This question already has answers here:
Convert date to another timezone in JavaScript
(34 answers)
How to initialize a JavaScript Date to a particular time zone
(20 answers)
Closed 3 years ago.
I need to convert from one timezone to another timezone in my project.
I am able to convert from my current timezone to another but not from a different timezone to another.
For example I am in India, and I am able to convert from India to US using Date d=new Date(); and assigning it to a calendar object and setting the time zone.
However, I cannot do this from different timezone to another timezone. For example, I am in India, but I am having trouble converting timezones from the US to the UK.
The easy and fast way is to use an external library. I recommend moment.js. once installed you can convert pretty easily and reliably through their ready-made functions

Create New JavaScript Date Object With Different Timezone During Initialization [duplicate]

This question already has answers here:
How to initialize a JavaScript Date to a particular time zone
(20 answers)
Closed 9 years ago.
I have seen a lot, really a lot of post to find out a solution for my problem, but i couldn't get it, so decided to create a question.
My Question is how do we actually create a new javascript date object in different timezone, not in local timezone
i know we could create local date object and convert it to different timezone in number of ways, but i dont want to convert instead i need to create in specific timezone.
here is simple example for my problem,
Say, user has choosen a timezone "America/New_york", so all the dates in a calendar page will be shown in that timezone.
Now, if we create a event at "05:00 pm" , how do we actually create date with time 5 pm in "America/New_york" timezone,
if we use new Date() (assume browser is in different timezone say "Asia/Kolkata"), then converting it to "America/New_york" will not get "5:00pm" in that timezone , instead it will get corresponding time of "05:00 pm IST" in that timezone which will have different hour & minute value.
Any suggestion would be helpful!
Thanks
The short answer is you can't.
A Date object is just an accessor to the system time settings (so it will use the local computer timezone anyway). You can then manipulate your dates by substracting the local timezone using getTimezoneOffset(), or forcing a time with setUTCHours().
Note that moment.js is a good alternative to handle dates and timezones: http://momentjs.com/

JavaScript compare different date formats?

I am trying to extract different date formats from a text and later compare those dates if they belong in some time span. Let' say time span is from 1.1.2013 to 1.3.2013 . This is DD/MM/YYYY time format.
Now how can I extract different time formats from text. I have here examples of time formats.
Tue Oct 23, 2012 7:59 am
February 19th, 2013, 07:32 PM
Today, 09:22 PM
Yesterday, 09:03 AM
28 February 2013 09:38
Yesterday 16:48
8 Oct 2012 5:41:00 AM
02-18-2013, 03:17 PM
02-01-13, 12:31 PM
12.2.2013 20:43
I understand this is not a simple task to perform but any kind of suggestion can help me.
Also I am aware of this question and answers . This will benefit me later
Compare two dates with JavaScript
Also the guys on chat had this to say.
Uwe Günther
#IceD looks like you need some lib who implements that all :-)
But ih ave noe clue which one does. Better ask the Stack
IMPORTANT
I don't want jQuery in this since I can't implement it in what I am using right now. So only JS solutions for this.
You will need to normalize them to a common, comparable format, most suitably Date objects.
For some of your formats, (some browsers) will be able to Date.parse them, but for others (like "yesterday") you need to do more sophisticated parsing (up to NLP?). It would be best if you knew the format of each snippet so you can pass them to the right parsing algorithm, if not you'll need to apply some heuristics.

Javascript Date.parse bug when dash-delimited and starts with year

Am seeking confirmation if this is a bona fide documentation and/or implementation bug with Javascript's Date.parse method.
The docs I'm referring to are at https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date/parse and they say 'If you do not specify a time zone, the local time zone is assumed.'
But the following code shows that, despite not specifying a time zone, local time is not being assumed (but rather my timezone offset is being applied), if the string passed to Date.parse begins with the 4-digit year representation, and is dash-delimited.
var euroStyleDate = '2011-10-04';
var amerStyleDate = '10/04/2011';
var euroStyleParsed = Date.parse(euroStyleDate);
var amerStyleParsed = Date.parse(amerStyleDate);
console.log(euroStyleParsed); //1317686400000
console.log(amerStyleParsed); //1317700800000
console.log(new Date(euroStyleParsed));
//Date {Mon Oct 03 2011 20:00:00 GMT-0400 (Eastern Daylight Time)}
console.log(new Date(amerStyleParsed));
//Date {Tue Oct 04 2011 00:00:00 GMT-0400 (Eastern Daylight Time)}
There may even be other cases, and I'm sure I'm not the first to discover this if I am incorrect. So beyond confirmation, I'd surely love to be pointed at more in-depth information on this if anybody knows of pertinent links.
I'm experiencing this in FF3, Chrome for Windows and of course just to be special IE8 doesn't even seem to able to perform the conversion on 2011-10-04 whatsoever: I'm just getting an empty string in my application
Thanks in advance for any further insight or resources.
I ran into this concept, too. For anyone googling "Javascript dates dashes slashes" like I was, this is the clearest demonstration that I can think of as to what's going on here.
In short, slashes means local time zone, and dashes means UTC. Other answers has explanations regarding why.
<script type="text/javascript">
var
testB = new Date("2012/02/09"),
testC = new Date("2012-02-09");
alert(testB.toString());
alert(testC.toString());
alert(testC.toUTCString());
</script>
**Update:**It looks like there are several different standards at work here:
The EMCAScript < 5 standard allowed for dates in the standard IETF format, e.g. Sun Oct 03 2010. With these dates, the local timezone is assumed.
In ECMAScript 5, a limited version of the ISO 8601 standard is also allowed, e.g. 2010-10-03. The spec seems to say (perhaps following ISO 8601?) that in this case, the UTC timezone is assumed if one is not specified.
I haven't found a spec that says Date.parse can handle mm/dd/yyyy dates, but my browser (Chrome 14) clearly can, and probably other browsers can too. This appears to follow standard 1 above, assuming the local timezone. Given that it's not in the spec, however, I would recommend not using this version, as it's likely to be browser-dependent (and I have no idea whether 10-03-2010 would result in a different date if I had a European locale set on my browser).
There are a few issues with the native Date.parse function in most interpreters - I have often had timezone issues like the one you describe. So in general, I either use a library like Datejs or I write my own parsing functions. The DateTime module of the SIMILE AJAX library has a pretty good example function for parsing ISO-8601 dates (what you're referring to as euroStyleDate, plus an optional time component).
When setting dates, I generally use new Date() and then use the setUTC*() functions to set the different date elements to my desired precision. It's not perfect, but at least you're dealing with a clear timezone.

Categories

Resources