When the date is passed from my c# to JavaScript it returns the date time as {4/3/2020 12:00:00 AM}
but in JavaScript it is shown as 1585852200000.
What is the format that is being used? And how can i convert it back?
You need to convert the Unix timestamp to DateTime format,
var localDate = new Date(1585852200000).toLocaleDateString("en-US")
console.log(localDate); // only local date
var localTime = new Date(1585852200000).toLocaleTimeString("en-US")
console.log(localTime) // only local time
// local datetime
console.log(new Date(1585852200000).toLocaleString());
1585852200000 is epoch date.
you can convert it as
var date = new Date(1585852200000)
console.log(new Date(1585852200000));
As an alternative from Shivaji's answer:
When you are passing the date through to JS you could cast it as a string with DateTime.ToString("dd/MM/yyyy") seen here on MSDN.
This will keep its integrity visually, if it is just for display purposes, otherwise you will need to re-cast appropriately in JS (in which case use Shivaji's answer).
JavaScript Date's object will return the DATE object and it's POSITION that is being assigned in your computer. So, when you are working with a date or datetime types, you can use some of the methods that are provided by the Date object, such as getDate() and getDay(). But, a better solution would be to format the Date object itself. For example: use the toString() or toUTCString() methods.
var d = new Date();
document.getElementById("demo").innerHTML = d.toString();
Reference:
https://www.w3schools.com/js/js_date_formats.asp
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
Related
I am building an online store, most of my customers (basically all) are located in a given timezone, but my infrastructure is located in other timezone (we can assume it's UTC). I have the option for my clients to select a date for their orders, the problem is that my date component represents dates like this "YYYY-MM-DD". In am using the Date constructor like this:
let dateString = "2019-06-03"
let date = new Date(dateString)
console.log(date) //This will print the local time zone representation of my dateString
console.log(date.toISOString()) //This will print the utc equivalent of my dateString
The problem with this is that I want the UTC representation to be calculated from the local timezone, not the other way around. Let's suppose in am located in GMT-5, when I say let date = new Date("2019-06-06") I want to see "2019-06-03T00:00:00.000 GMT-5" , and the ISOString should be "2019-06-03T05:00:00.000Z". How can I do this ?
What you are trying to achieve can be done by appending the string T00:00:00 to the dateString before passing it to the Date() constructor.
But a word of caution, manipulating the timezone/offsets manually like this might result in incorrect data being presented.
If you are storing and retrieving all the order timestamps in UTC only, it will avoid timezone related issues and you might not need to process the timestamps like this.
let dateString = "2019-06-03"
let date = new Date(dateString + "T00:00:00")
console.log(date) //This will print the local time zone representation of my dateString
console.log(date.toISOString()) //This will print the utc equivalent of my dateString
In javascript I have some datetime like this
Date: '2017-07-04'
I want to convert it to DateTime like ajax get result.
Expect result like this:
'/Date(1565089870830)/'
How can I make it possible?
You can use Date.parse(). This method parses a string representation of a date, and returns the number of milliseconds since January 1, 1970, 00:00:00 UTC or NaN if the string is unrecognized or, in some cases, contains illegal date values (e.g. 2015-02-31).
var date = Date.parse('2017-07-04');
console.log(date);
The format you're trying to create is a string containing an Epoch timestamp. To create that in JS you can create a Date object from the input string and retrieve the getTime() property. Then it's just a matter of concatenating that value in to the format needed. Try this:
var date = new Date('2017-07-04');
var epoch = date.getTime();
var output = `/Date(${epoch})/`;
console.log(output);
Presumably you're working with an ASP.Net MVC site, given the date format you're trying to build. One thing to note here is that you don't need to use that format when sending DateTime values back to the server. You can send any string so long as it can be bound to a DateTime instance by the ModelBinder. As such I'd recommend using an ISO8601 format instead.
I am not so into JavaScript and I have the following problem.
I have a JSON object like this:
{
"start_date": "2017-11-09 06:00:00"
}
Into a JavaScript script executed into the browser I do:
var dateCurrentOriginalForecast = new Date(currentOriginalForecast.start_date);
and it works fine: it creates a new Date object with the value related to 2017-11-09 06:00:00 date.
The problem is that I have to perform this JavaScript script into a Java application using Rhino (a JavaScript implementation that allows to perform JS code into a Java application) and here it cause an error:
TID: [-1234] [] [2017-11-09 11:10:08,915] INFO {org.apache.synapse.mediators.bsf.ScriptMessageContext} - dateCurrentOriginalForecast: Invalid Date {org.apache.synapse.mediators.bsf.ScriptMessageContext}
TID: [-1234] [] [2017-11-09 11:10:08,918] ERROR {org.apache.synapse.mediators.bsf.ScriptMediator} - The script engine returned an error executing the inlined js script function mediate {org.apache.synapse.mediators.bsf.ScriptMediator}
com.sun.phobos.script.util.ExtendedScriptException: org.mozilla.javascript.EcmaError: RangeError: Date is invalid. (<Unknown Source>#137) in <Unknown Source> at line number 137
at com.sun.phobos.script.javascript.RhinoCompiledScript.eval(RhinoCompiledScript.java:68)
at javax.script.CompiledScript.eval(CompiledScript.java:92)
It seems that this date is invalid and it can't create the Date object.
From what I understood reading online the problem should be that old JS or Rhino (maybe the version of JS implemented by Rhino) does not support date of this type and probably I have to convert it in a date format which is fully compliant with ISO 8601
So I think that I have to convert my string 2017-11-09 06:00:00 into something like compliant with ISO 8601 standard.
I can't use third party library.
How can I do it?
Can use Date#toISOString() or Date#toJSON()
let d = new Date('2017-11-09 06:00:00')
console.log(d.toISOString())
console.log(d.toJSON())
//if you want convert date without convert in timezone than
var date = '2017-11-09 06:00:00';
var convertDate = date.replace(" ", "T"); // 2017-11-09T06:00:00
//if you want to convert in date with utc timezone
var date = new Date("2017-11-09 06:00:00").toISOString()
If I've understood your question correctly the problem is not so much that you need a ISO 8601 formatted date, but it is that you need to create a Date object from a date that is not formatted in ISO 8601. I personally would just use regular expression to parse the date into it's parts and then pass them into the Date constructor:
var currentOriginalForecast = {
"start_date": "2017-11-09 06:00:00"
};
var rxParseDate = /(\d{4})-(\d\d)-(\d\d)\s+(\d\d):(\d\d):(\d\d)/;
var dateParts = currentOriginalForecast.start_date.match(rxParseDate);
var year = dateParts[1],
month = dateParts[2],
day = dateParts[3],
hour = dateParts[4],
minute = dateParts[5],
second = dateParts[6];
var dateCurrentOriginalForecast = new Date(Date.UTC(year, month - 1, day, hour, minute, second));
console.log(dateCurrentOriginalForecast);
Since there is no timezone mentioned in the start_date, I'm assuming it is UTC and converting it using Date.UTC and passing the resulting timestamp from that into the Date constructor. If start_date is in local time you would just remove Date.UTC and pass the parameters directly into the Date constructor. I'll also mention the month - 1; that is because the Date constructor (and Date.UTC) expect a 0-based month.
I have a date time string in format ("2015-10-07 15:20:00 UTC") and i want to convert it to local time zone of client. i am using the following statements for this:
var UTC_Time = new Date ("2015-10-07 15:20:00 UTC");
var localTime = UTC_Time.toString();
in Google Chrome it works fine and return the converted time as 2015-10-07 20:20:00 PST which is fine. But in internet explorer (i am concerned with IE10) it is returning the same UTC date i.e. 2015-10-07 15:20:00. how can i get the converted time in IE. Any help would be greatly appreciated.
When you display a date in javascript, it converts it to the client time. Since you are specifying UTC in your date string, it will assume that it's a UTC date. There are a couple ways you can solve this.
If you just need a string, you can do localTime = UTC_Time.toUTCString().
If you need a js Date object, you can create a new date object by getting the values from the previous object.
new Date(UTC_Time.getUTCFullYear(), UTC_Time.getUTCMonth(),
UTC_Time.getUTCDate(), UTC_Time.getUTCHours(), UTC_Time.getUTCMinutes(),
UTC_Time.getUTCSeconds(), UTC_Time.getUTCMilliseconds());
Or you can simply replace the UTC part of the string.
var dtStr = "2015-10-07 15:20:00 UTC";
dtStr = dtStr.replace(" UTC", "");
var localTime = new Date(dtStr);
Only use this option if you know your string will always be in the same format.
I have a UTC date string like this "2013-08-22T00:35:00", how do I use Javascript to convert it to a local computer time? so after conversion the time should be 2013-08-21 20:35
Thanks
Does
new Date("2013-08-22T00:35:00")
work for you?
MDN
Or just use libraries designed for that, like moments.js
moment("2013-08-22T00:35:00");
JavaScript convert datetime string to local on creating new instance.
var date = new Date("2013-08-22T00:35:00");
date.toString() //local date
Here is demo