Datetime to JavaScript Date

DateTime to javascript date

Try:

return DateTime.Now.Subtract(new DateTime(1970, 1,1)).TotalMilliseconds

Edit: true UTC is better, but then we need to be consistent

return DateTime.UtcNow
.Subtract(new DateTime(1970,1,1,0,0,0,DateTimeKind.Utc))
.TotalMilliseconds;

Although, on second thoughts it does not matter, as long as both dates are in the same time zone.

Convert datetime to valid JavaScript date

This works everywhere including Safari 5 and Firefox 5 on OS X.

UPDATE: Fx Quantum (54) has no need for the replace, but Safari 11 is still not happy unless you convert as below

var date_test = new Date("2011-07-14 11:23:00".replace(/-/g,"/"));console.log(date_test);

Convert C# DateTime to Javascript Date

Given the output you're stuck with, I can't think of any better way to catch a DateTime of 0 on the javascript side.

Date.parse should work for your parsing needs, but it returns number of milliseconds, so you need to wrap a Date constructor around it:

var date = new Date(Date.parse(myCSharpString));

For the return date, you simply want

date.getFullYear() + "/" + (date.getMonth() + 1) + "/" + (date.getDate() + 1);

(date.getMonth and date.getDate are 0-indexed instead of 1-indexed.)

Fiddle: http://jsfiddle.net/GyC3t/

EDIT
Thanks to JoeB's catch, let me do a correction. The date.getMonth() function is 0-indexed, but the date.getDate() function is 1-indexed. The fiddle was "working" with the +1 because date.getMonth works in local time, which is before UTC. I didn't properly check the docs, and just added 1, and it worked with the fiddle.

A more proper way to do this is:

For the return date, you simply want

date.getFullYear() + "/" + (date.getMonth() + 1) + "/" + (date.getUTCDate());

(date.getMonth is 0-indexed while date.getDate is 1-indexed but susceptible to time-zone differences.)

Fiddle: http://jsfiddle.net/GyC3t/25/

Convert DateTime to Date in ES6

I also don't like using libraries for every tasks that can be easily done without library.Here are some of the in-build JS methods that you must give a try:

let date1=new Date("2020-06-16T02:55:08.151437Z").toDateString()console.log(date1)
let date2=new Date("2020-06-16T02:55:08.151437Z").toUTCString()console.log(date2)
let date3=new Date("2020-06-16T02:55:08.151437Z").toLocaleDateString()console.log(date3)
let date4=new Date("2020-06-16T02:55:08.151437Z").toLocaleDateString('en-US', {month: '2-digit',day: '2-digit',year: 'numeric'})console.log(date4)
let date5=new Date("2020-06-16T02:55:08.151437Z").toString()console.log(date5)
let date6=new Date("2020-06-16T02:55:08.151437Z").toLocaleString()console.log(date6)

How to remove time part from Date?

Split it by space and take first part like below. Hope this will help you.

var d = '12/12/1955 12:00:00 AM';
d = d.split(' ')[0];
console.log(d);

Convert UTC date time to local date time

Append 'UTC' to the string before converting it to a date in javascript:

var date = new Date('6/29/2011 4:52:48 PM UTC');
date.toString() // "Wed Jun 29 2011 09:52:48 GMT-0700 (PDT)"

How to convert Javascript datetime to C# datetime?

First create a string in your required format using the following functions in JavaScript

var date = new Date();
var day = date.getDate(); // yields date
var month = date.getMonth() + 1; // yields month (add one as '.getMonth()' is zero indexed)
var year = date.getFullYear(); // yields year
var hour = date.getHours(); // yields hours
var minute = date.getMinutes(); // yields minutes
var second = date.getSeconds(); // yields seconds

// After this construct a string with the above results as below
var time = day + "/" + month + "/" + year + " " + hour + ':' + minute + ':' + second;

Pass this string to codebehind function and accept it as a string parameter.Use the DateTime.ParseExact() in codebehind to convert this string to DateTime as follows,

DateTime.ParseExact(YourString, "dd/MM/yyyy HH:mm:ss", CultureInfo.InvariantCulture);

Hope this helps...

Convert JS date time to MySQL datetime

var date;
date = new Date();
date = date.getUTCFullYear() + '-' +
('00' + (date.getUTCMonth()+1)).slice(-2) + '-' +
('00' + date.getUTCDate()).slice(-2) + ' ' +
('00' + date.getUTCHours()).slice(-2) + ':' +
('00' + date.getUTCMinutes()).slice(-2) + ':' +
('00' + date.getUTCSeconds()).slice(-2);
console.log(date);

or even shorter:

new Date().toISOString().slice(0, 19).replace('T', ' ');

Output:

2012-06-22 05:40:06

For more advanced use cases, including controlling the timezone, consider using http://momentjs.com/:

require('moment')().format('YYYY-MM-DD HH:mm:ss');

For a lightweight alternative to momentjs, consider https://github.com/taylorhakes/fecha

require('fecha').format('YYYY-MM-DD HH:mm:ss')

Parsing a string to a date in JavaScript

The best string format for string parsing is the date ISO format together with the JavaScript Date object constructor.

Examples of ISO format: YYYY-MM-DD or YYYY-MM-DDTHH:MM:SS.

But wait! Just using the "ISO format" doesn't work reliably by itself. String are sometimes parsed as UTC and sometimes as localtime (based on browser vendor and version). The best practice should always be to store dates as UTC and make computations as UTC.

To parse a date as UTC, append a Z - e.g.: new Date('2011-04-11T10:20:30Z').

To display a date in UTC, use .toUTCString(),

to display a date in user's local time, use .toString().

More info on MDN | Date and this answer.

For old Internet Explorer compatibility (IE versions less than 9 do not support ISO format in Date constructor), you should split datetime string representation to it's parts and then you can use constructor using datetime parts, e.g.: new Date('2011', '04' - 1, '11', '11', '51', '00'). Note that the number of the month must be 1 less.


Alternate method - use an appropriate library:

You can also take advantage of the library Moment.js that allows parsing date with the specified time zone.

How do I format a date in JavaScript?

For custom-delimited date formats, you have to pull out the date (or time)
components from a DateTimeFormat object (which is part of the
ECMAScript Internationalization API), and then manually create a string
with the delimiters you want.

To do this, you can use DateTimeFormat#formatToParts. You could
destructure the array, but that is not ideal, as the array output depends on the
locale:

{ // example 1
let f = new Intl.DateTimeFormat('en');
let a = f.formatToParts();
console.log(a);
}
{ // example 2
let f = new Intl.DateTimeFormat('hi');
let a = f.formatToParts();
console.log(a);
}


Related Topics



Leave a reply



Submit