Extract date from long complicated date plus time stamp? - javascript

I am using bootstrap calendar and on clicking any particular date I got complicated date in this form:
Sat Sep 06 2014 00:00:00 GMT 0500 (Pakistan Standard Time)
Can anybody please tell me how I can extract only date from this complicated long date?
I am working in PHP codeignitor, is there any way in PHP or JavaScript through which I can only extract date?

if this is for Javascript then just use this:
var myDate = new Date('Sat Sep 06 2014 00:00:00 GMT 0500 (Pakistan Standard Time)');
after that you can use myDate as a date object and call its methods like myDate.getFullYear() etc.

Related

DateTime value from backend to frontend

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)"

When I try to create a date object from another date format, the result date is changing it's value

When I try to create a date object from another date format, the result date is changing it's value. How to achieve this without changing the date value ?
new Date("Mon, 31 Oct 2016 00:00:00 GMT");
the result coming as Sun Oct 30 2016 20:00:00 GMT-0400 (Eastern Daylight Time), How can I get the Monday 31 date from the above?
Adjusting the timezoneOffset from the created date object should do the trick,
but be cautious while using it , as you should be sure that the date object was created from GMT not from some local time .
And the below answer has been posted assuming the input date was in GMT
var tempDate = new Date("Mon, 31 Oct 2016 00:00:00 GMT");
var tempTime = tempDate.getTime() + (tempDate.getTimezoneOffset() * 60000);
tempDate = new Date(tempTime);
console.log(tempDate);
It doesn't change the date, it just converts it to your local timezone. This is a bit of annoying behaviour and the only way I know to get around it is to set your system timezone to GMT. If you need to do date and time work, you might want to look at Moment.js - http://momentjs.com/

convert month string to date

I'm using some library that won't sort objects by a string value but will sort them by date. I have months like '2008-04' and I should be able to convert them to Javascript dates for the first of the appropriate month. But my local timezone screws things up:
new Date('2008-04')
Mon Mar 31 2008 20:00:00 GMT-0400 (EDT)
This is probably a duplicate of How do you convert a JavaScript date to UTC?, but maybe there's a simpler answer for my particular use case than the ones there?
BTW, I get the same answer by specifying the first of the month:
new Date('2008-04-01')
Mon Mar 31 2008 20:00:00 GMT-0400 (EDT)
I'm using ES6. I don't suppose that makes it any more straightforward?
Add '-01T00:00:00Z' to the string with part of ISO 6801 date:
document.write(new Date('2008-04' + '-01T00:00:00Z'));
I misunderstood. The date is already UTC, it's just when I display it as a string locally that it gets converted to my local timezone. So the answer is just
new Date('2008-04').toUTCString()
"Tue, 01 Apr 2008 00:00:00 GMT"
or
new Date('2008-04').toISOString()
"2008-04-01T00:00:00.000Z"

Convert indian standard time to mysql datetime format using php

I have my indian standard time like ,
Wed Oct 08 2014 07:40:00 GMT 0530 (India Standard Time) , which is created by javascript,
Now i want to change this format to 2014-11-10 07:40:00 to store in mysql.
how can i do this.
I hope this will help you
$date = "Wed Oct 08 2014 07:40:00";
echo date('Y-m-d h:i:s',strtotime($date));
output
2014-10-08 07:40:00
You can use the moment.js library.
var a = "Wed Oct 08 2014 07:40:00 GMT 0530 (India Standard Time)";
var b = moment(a).zone(0).format(YYYY-MM-DD HH:mm:ss);
I personally would transform your time with the time()-function to the unix format amd with then with date() back to your liking:
http://php.net/manual/de/function.date.php
i.g.: date('Y-m-d H:i:s',time()) would give you the momentary time in the desired format.
edit: I forget the damn '...', but I corrected that. The accepted answer ist correct and a little bit better than mine.

Date-time stamp conversion in javascript

I have Date String Thu May 23 2013 18:19:32 GMT+0530 (India Standard Time) from my database. I want to make in this format THURSDAY May 23 2013 18:19:32 GMT 0500 CDT in ext-js.any idea ? Thanks in advance.
There are 2 excellent ate parsing libraries available tat you can use. They are both very small
https://code.google.com/p/datejs/
http://momentjs.com/
Sample datejs usage:
Date.parse('Thu, 1 July 2004 22:30:00 GMT') // Thu Jul 01 2004 16:30:00
You can then format the date object in whatever format you require for output.

Categories

Resources